注册表操做

注册表实例(开机自启动)ide

 1         private void Form1_Load(object sender, EventArgs e)
 2         {
 3             //判断此健值是否存在,存在则选中
 4             if (IsExistKey(System.IO.Path.GetFileNameWithoutExtension(Application.ExecutablePath)))
 5             {
 6                 checkBox1.Checked = true;
 7             }
 8             else
 9             {
10                 checkBox1.Checked = false;
11             }
12         }
13         private void checkBox1_Click(object sender, EventArgs e)
14         {
15             //应该程序的路径 
16             string keyValue = Application.ExecutablePath;
17             string keyName = System.IO.Path.GetFileNameWithoutExtension(keyValue);
18             if (this.checkBox1.CheckState == CheckState.Checked)
19             {
20                 //设置开机自动运行的值,对应的路径(如C:Program FilesWinRARWinRAR.exe)              
21                 WriteKey(keyName, keyValue);
22             }
23             else
24             {
25                 DeleteKey(keyName);
26             }
27         }
28         private bool IsExistKey(string keyName)
29         {
30             bool _exist = false;
31             RegistryKey hklm = Registry.LocalMachine;
32             RegistryKey runs = hklm.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
33             //注意此处用的是GetValueNames() 
34             string[] runsName = runs.GetValueNames(); foreach (string strName in runsName)
35             {
36                 if (strName.ToUpper() == keyName.ToUpper())
37                 {
38                     _exist = true; return _exist;
39                 }
40             }
41             return _exist;
42         }
43 
44         private bool WriteKey(string keyName, string keyValue)
45         {
46             RegistryKey hklm = Registry.LocalMachine;
47             //定义hklm指向注册表的LocalMachine,其中 Software\Microsoft\Windows\CurrentVersion\Run就是关系到系统中随系统启动而启动的程序,通称启动项                
48             RegistryKey run = hklm.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
49             try
50             {
51                 //将咱们的程序加进去                   
52                 run.SetValue(keyName, keyValue);
53                 //注意,必定要关闭,注册表应用。                  
54                 hklm.Close();
55                 return true;
56             }
57             catch (Exception)
58             {
59                 return false;
60             }
61         }
62         //删除键值          
63         private void DeleteKey(string keyName)
64         {
65             RegistryKey hklm = Registry.LocalMachine; 
66             RegistryKey runs = hklm.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
67             try
68             {
69                 //注意此处用的是GetValueNames() 
70                 string[] runsName = runs.GetValueNames();
71                 foreach (string strName in runsName)
72                 {
73                     if (strName.ToUpper() == keyName.ToUpper())
74                         runs.DeleteValue(strName, false);
75                 }
76             }
77             catch (Exception ex)
78             {
79                 MessageBox.Show(ex.Message);
80             }
81         }
82         
83     }
开机自启动

修改WebBrowser内核this

public class OperateRegedit
    {
        //C#操做注册表 
        //1.读取指定名称的注册表的值
        public string GetRegistData(string name)
        {
            string registData;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.OpenSubKey(@"Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION\基站信息采集分析系统.exe" ,true);
            registData = aimdir.GetValue(name).ToString();
            return registData;
        }
        //以上是读取的注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下的XXX目录中名称为name的注册表值;
        //2.向注册表中写数据
        public void WTRegedit(string name, string tovalue)
        {
            RegistryKey hklm = Registry.LocalMachine;
            RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.CreateSubKey(@"Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION\基站信息采集分析系统.exe");
            aimdir.SetValue(name, tovalue);
        }
        //以上是在注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下新建XXX目录并在此目录下建立名称为name值为tovalue的注册表项;
        //3.删除注册表中指定的注册表项
        private void DeleteRegist(string name)
        {
            string[] aimnames;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.OpenSubKey(@"Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true);
            aimnames = aimdir.GetSubKeyNames();
            foreach (string aimKey in aimnames)
            {
                if (aimKey == name)
                    aimdir.DeleteSubKeyTree(name);
            }
        }
        //以上是在注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下XXX目录中删除名称为name注册表项;
        //4.判断指定注册表项是否存在
        public bool IsRegeditExit(string name)
        {
            bool _exit = false;
            try
            {
                string[] subkeyNames;
                RegistryKey hkml = Registry.LocalMachine;
                RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
                RegistryKey aimdir = software.OpenSubKey(@"Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true);
                subkeyNames = aimdir.GetValueNames();
                foreach (string keyName in subkeyNames)
                {
                    if (keyName.ToLower() == name.ToLower())
                    {
                        _exit = true;
                        return _exit;
                    }
                }
            }
            catch
            { }
            return _exit;
        }
        //以上是在注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下XXX目录中判断名称为name注册表项是否存在,这一方法在删除注册表时已经存在,在新建一注册表项时也应有相应判断; 

        public void WTRegeditAutoRun(string name,int tovalue)
        {
            using (RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true))
            {
                try
                {
                    runKey.SetValue(name, tovalue);
                }
                catch (Exception)
                {
                }
               
            }
            using (RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true))
            {
                try
                {
                    var subkeyNames = runKey.GetValueNames();
                    foreach (string keyName in subkeyNames)
                    {
                        if (keyName.ToLower() == name.ToLower())
                        {
                            return;
                        }
                    }
                    runKey.SetValue("基站信息采集分析系统.exe", tovalue);
                }
                catch (Exception)
                {
                }

            }
        }

        public  void RunWhenStart(bool Started, string name, string path)
        {
            RegistryKey HKLM = Registry.LocalMachine;
            RegistryKey Run = HKLM.CreateSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION");
            if (Started == true)
            {
                try
                {
                    Run.SetValue(name, path);
                    HKLM.Close();
                }
                catch (Exception Err)
                {
                    //MessageBox.Show(Err.Message.ToString());
                }
            }
            else
            {
                try
                {
                    Run.DeleteValue(name);
                    HKLM.Close();
                }
                catch (Exception)
                {
                    // 
                }
            }
        }  

        // 8、RunOnce\Setup注册键 
        //RunOnce\Setup指定了用户登陆以后运行的程序,它的位置是:HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce\Setup,和HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\Setup。  LocalMachine

    }
OperateRegedit

调用OperateRegeditspa

                string exeName = Application.ExecutablePath;
                exeName = exeName.Substring((exeName.LastIndexOf(@"\") + 1));
                OperateRegedit op = new OperateRegedit();
                if (!op.IsRegeditExit(exeName))
                {
                    op.WTRegeditAutoRun(exeName, 10000);
                }
调用