1. 直接上代码(写在需要设置开机自启动的位置)
string path = Application.ExecutablePath;
RegistryKey rk =Registry.LocalMachine;
RegistryKey rk2 =rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.SetValue("[自定义名字]",path);
rk2.Close();
rk.Close();
2. 无法启动问题分析(查看注册表已经正确修改了,但是开机仍然没有启动)
a. 系统路径获取问题,查看项目中是否有相对路径或是否有错误的获取项目路径的方式
(System.Environment.CurrentDirectory)× 获取当前路径
使用这个获取方式,开机自启动后当前路径是(C:\windows\system32)
可以更改为(System.Windows.Forms.Application.StartupPath)√ 获取当前路径
b. 查看是