1、新建一个类,设置一个操作注册表的方法;
2、在程序中调用这个方法。true就自动启动,false就删除自动启动。
- public static bool RunWhenStart(bool started, string exeName, string path)
- {
- RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
- if (key == null)
- {
- key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
- }
- if (started == true)
- {
- try
- {
- key.SetValue(exeName, path);
- key.Close();
- }
- catch
- {
- return false;
- }
- }
- else
- {
- try
- {
- key.DeleteValue(exeName);
- key.Close();
- }
- catch
- {
- return false;
- }
- }
- return true;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- if (Autorun == "true")
- {
- function.RunWhenStart(true, "51cto.iego.net", Application.ExecutablePath);
- }
- else
- {
- function.RunWhenStart(false, "51cto.iego.net", Application.ExecutablePath);
- }
- }