代码改变世界

关于C# Winform 程序开机自动启动

2014-12-18 14:21  Evan.Pei  阅读(293)  评论(0编辑  收藏  举报

1.程序运行时调用下面方法即可。

 /// <summary>  
        /// 设置开机自动启用  
        /// </summary>  
        private void SetAutoStart()
        {
            try
            {
                string regPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";//注册表
                string path = Application.ExecutablePath.ToLower(); //获取.exe当前程序路径  
                //MessageBox.Show(path);
                string name = Path.GetFileName(path);  //获得应用程序名称  
                //MessageBox.Show(name);
                var regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regPath, true);
                if (regKey == null) regKey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(regPath);
                regKey.SetValue(name, path);
            }
            catch
            {
            }
        }