winform程序如何做到开机自动启动

如下一个方法就搞定了

        /// 
        /// 开机自启
        /// 
        public static void Fun_AutoStart(bool isAutoRun = true)
        {
            try
            {
                string path = Application.ExecutablePath;
                RegistryKey rk = Registry.LocalMachine;
                RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
                if (isAutoRun)
                    rk2.SetValue("System Security", path); //rk2.DeleteValue("OIMSServer", false);
                else
                    rk2.DeleteValue("System Security", false);
                rk2.Close();
                rk.Close();
            }
            catch
            {
                MessageBox.Show("开机自动启动服务注册被拒绝!请确认有系统管理员权限!");
            }
        }

调用方式:

Fun_AutoStart(true)

 

你可能感兴趣的:(C#)