【转】添加ClickOnce程序的快捷方式 ,自动运行

另2篇文章:

http://m.cnblogs.com/23343/1057764.html

 

标题: 添加ClickOnce程序的快捷方式
- 葵花子 2008-01-29 16:53 阅读:320
- 评论:1 查看评论 | 添加评论 | 返回
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("本程序只允许同时运行一个!");
            }

        }

    }
}

另一篇:
在ClickOnce应用程序中创建桌面快捷方式

http://www.cnblogs.com/teamleader/articles/1193466.html

 

posted @ 2010-04-22 10:58  上善若水-water  Views(1710)  Comments(0)    收藏  举报