您的位置:首页 > 其它

添加ClickOnce程序的快捷方式

2008-01-29 16:53 351 查看
using System;

using System.Collections.Generic;

using System.Windows.Forms;

using Microsoft.Win32;

using IWshRuntimeLibrary;

namespace MyProgram

{

static class Program

{

/// <summary>

/// 应用程序的主入口点。

/// </summary>

[STAThread]

static void Main(string[] args)

{

// 显示Splash窗体

Splash.Show();

DoStartup(args);

// 关闭Splash窗体

Splash.Close();

}

static void DoStartup(string[] args)

{

// 做需要的事情

//开机自启动

string startpath = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\程序\\我的程序\\程序快捷方式.appref-ms";

RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

string sRtn = key.GetValue("myprog", "notexist").ToString();

if (sRtn == "notexist")

{

key.SetValue("myprog", startpath);

}

else

{

if (sRtn != startpath)

key.SetValue("myprog", startpath);

}

//创建快捷方式

WshShell shell = new WshShell();

IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\客户端.lnk");

//string spath = System.Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);

//spath = spath + "\\程序\\我的程序\\程序的快捷方式";

//shortcut.TargetPath = spath;//Application.ExecutablePath;

//shortcut.TargetPath = Application.ExecutablePath;

shortcut.TargetPath = startpath;

//shortcut.WorkingDirectory = System.Environment.CurrentDirectory;

shortcut.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\程序\\我的程序";

shortcut.WindowStyle = 1;

shortcut.Description = "我的程序的快捷方式";

shortcut.Save();

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

fMain mainfrm = new fMain();

myform.CustomerForm = mainfrm;

Boolean createdNew;

System.Threading.Mutex m = new System.Threading.Mutex(true, "myprog", out createdNew);

if (createdNew)

{

Application.Run(mainfrm);

m.ReleaseMutex();

}

else

{

MessageBox.Show("本程序只允许同时运行一个!");

}

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: