註冊表操做

註冊表實例(開機自啓動)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);
                }
調用
相關文章
相關標籤/搜索