01 | private void RegCompStartRun( bool cmd) |
02 | { |
03 | //获取程序执行路径.. |
04 | string starupPath = Application.ExecutablePath; |
05 | //表示Window注册表中项级节点,读取 Windows 注册表基项HKEY_LOCAL_MACHINE |
06 | RegistryKey loca = Registry.LocalMachine; |
07 | RegistryKey run = loca.CreateSubKey( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run" ); |
08 | lblDisplay.Text += "执行成功!" ; |
09 | try |
10 | { |
11 | //SetValue:存储值的名称 |
12 | if (cmd) |
13 | { |
14 | run.SetValue( "WinForm" , starupPath); //加入注册 |
15 | } |
16 | else |
17 | { |
18 | run.DeleteValue( "WinForm" , false ); //删除该节点注册 |
19 | } |
20 | loca.Close(); |
21 | } |
22 | catch (Exception ee) |
23 | { |
24 | MessageBox.Show(ee.Message.ToString(), "提示" ,MessageBoxButtons.OK,MessageBoxIcon.Error); |
25 | } |
26 |
27 | } |
有了这个就可以使Winform程序开机自动运行。
联系客服