WinForm程序开机自动启动

void AutoStart()
        {
            try
            {
                string regPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
                string path = Application.ExecutablePath.ToLower(); //将当前程序起动路径  
                //MessageBox.Show(path);
                string name = System.IO.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
            {
            }
        }

以上是程序中直接写入注册表,可以在打开运行,输入:regedit 然后找到 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 下就可以看到已经被写入注册表,这样在开机时就会自动开启程序的。
原文地址:https://www.cnblogs.com/059212315/p/2371812.html