Loading

C#用注册表开机自动启动某某软件

代码如下:

     public static void chkAutoRun(bool isRun)
        {
            if (isRun)//开机自动启动 
            {
                try
                {
                    RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                    runKey.SetValue("AutoRun.exe", System.Windows.Forms.Application.ExecutablePath);
                    runKey.Close();
                    this.Text = "注册表修改自启(已开启)";
                }
                catch (IOException ie)
                {
                    MessageBox.Show(ie.Message);
                }
            }
            else  //不开机自动启动注册表信息删除 
            {
                RegistryKey software = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                string[] aimnames = software.GetValueNames();
                foreach (string aimKey in aimnames)
                {
                    if (aimKey.Equals("AutoRun.exe"))
                    {
                        software.DeleteValue("AutoRun.exe");
                        software.Close();
                        break;
                    }
                }
                this.Text = "注册表修改自启(已关闭)";
            } 

        }



 

posted @ 2016-01-06 10:10  myEsn2E9  阅读(224)  评论(0编辑  收藏  举报