windows程序设置开机自动启动

//调用方法:设置开机启动
SetAutoRun(Process.GetCurrentProcess().ProcessName, true, Application.StartupPath + @"\" + Process.GetCurrentProcess().ProcessName + ".exe");

 

 /// <summary>
        /// 设置为开机启动
        /// </summary>
        private void SetAutoRun(string keyName, bool isAutoRun, string filePath = "")
        {
            RegistryKey runKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
            if (runKey == null)
                runKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            if (isAutoRun)
                runKey.SetValue(keyName, filePath);
            else
                runKey.DeleteValue(keyName, false);
            runKey.Close();
        }

  

posted @ 2018-04-08 15:03  无处安放的青春  阅读(1408)  评论(0编辑  收藏  举报