方法一://From:www.uzhanbao.com /// <summary> /// 设置开机自动启动 /// </summary> void setAppAuto() { if (StaticModel.AppAutoStart) //设置开机自启动 { //获取程序路径 string execPath = Application.ExecutablePath; try { RegistryKey RKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); //设置自启的程序叫获取目录下的程序名字 //string[] ar = RKey.GetValueNames(); //foreach (string st in ar) //{ // if (st.Equals("ComputerCtrl89")) // { // } //} // 如果没有ComputerCtrl关键字,会自动创建,如果有就更新键值 RKey.SetValue("ComputerCtrl", execPath, RegistryValueKind.String); } catch (Exception ex) { MessageBox.Show("ex:" + ex.Message); } } else //取消开机自启动 { RegistryKey rk = Registry.LocalMachine; RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); rk2.DeleteValue("ComputerCtrl", false); rk2.Close(); rk.Close(); } }
如果遇到开启启动不能读取配置文件的话请看下面
请使用绝对路径
private static string configFilePath = "Config.xml"; public static string ConfigFilePath { get { configFilePath = Application.StartupPath + "\\Config.xml"; return ParserConfig.configFilePath; } set { ParserConfig.configFilePath = value; } } 方法二 也可以这样,复制一下文本, 改后缀为*.reg, 双击即可
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run] "CCtrl"="\"C:\\Program Files\\WisRoom\\CCtrl\\ComputerCtrl.exe\"" 上述中HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run 也可以去注册表中查找,手动添加
小经验1: 我 想让一个程序开机启动,win7上没问题,到了win10上,注册表添加成功了,但还是无法开机自动启动,程序安装后,快捷方式有个盾牌,快捷方式右键,查看我的“以管理员身份运行”,我是把勾它去掉的,后来查资料说,项目的app.manifest文件要改,改完了就好了,如下 ?