zoukankan      html  css  js  c++  java
  • 注册表操作

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

     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(@"SoftwareMicrosoftWindowsCurrentVersionRun", 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,其中 SoftwareMicrosoftWindowsCurrentVersionRun就是关系到系统中随系统启动而启动的程序,通称启动项                
    48             RegistryKey run = hklm.CreateSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRun");
    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(@"SoftwareMicrosoftWindowsCurrentVersionRun", 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内核

    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(@"MicrosoftInternet ExplorerMAINFeatureControlFEATURE_BROWSER_EMULATION基站信息采集分析系统.exe" ,true);
                registData = aimdir.GetValue(name).ToString();
                return registData;
            }
            //以上是读取的注册表中HKEY_LOCAL_MACHINESOFTWARE目录下的XXX目录中名称为name的注册表值;
            //2.向注册表中写数据
            public void WTRegedit(string name, string tovalue)
            {
                RegistryKey hklm = Registry.LocalMachine;
                RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);
                RegistryKey aimdir = software.CreateSubKey(@"MicrosoftInternet ExplorerMAINFeatureControlFEATURE_BROWSER_EMULATION基站信息采集分析系统.exe");
                aimdir.SetValue(name, tovalue);
            }
            //以上是在注册表中HKEY_LOCAL_MACHINESOFTWARE目录下新建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(@"MicrosoftInternet ExplorerMAINFeatureControlFEATURE_BROWSER_EMULATION", true);
                aimnames = aimdir.GetSubKeyNames();
                foreach (string aimKey in aimnames)
                {
                    if (aimKey == name)
                        aimdir.DeleteSubKeyTree(name);
                }
            }
            //以上是在注册表中HKEY_LOCAL_MACHINESOFTWARE目录下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(@"MicrosoftInternet ExplorerMAINFeatureControlFEATURE_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_MACHINESOFTWARE目录下XXX目录中判断名称为name注册表项是否存在,这一方法在删除注册表时已经存在,在新建一注册表项时也应有相应判断; 
    
            public void WTRegeditAutoRun(string name,int tovalue)
            {
                using (RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWAREWow6432NodeMicrosoftInternet ExplorerMAINFeatureControlFEATURE_BROWSER_EMULATION", true))
                {
                    try
                    {
                        runKey.SetValue(name, tovalue);
                    }
                    catch (Exception)
                    {
                    }
                   
                }
                using (RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWAREMicrosoftInternet ExplorerMAINFeatureControlFEATURE_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(@"SOFTWAREWow6432NodeMicrosoftInternet ExplorerMAINFeatureControlFEATURE_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)
                    {
                        // 
                    }
                }
            }  
    
            // 八、RunOnce\Setup注册键 
            //RunOnce\Setup指定了用户登录之后运行的程序,它的位置是:HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce\Setup,和HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\Setup。  LocalMachine
    
        }
    OperateRegedit

    调用OperateRegedit

                    string exeName = Application.ExecutablePath;
                    exeName = exeName.Substring((exeName.LastIndexOf(@"") + 1));
                    OperateRegedit op = new OperateRegedit();
                    if (!op.IsRegeditExit(exeName))
                    {
                        op.WTRegeditAutoRun(exeName, 10000);
                    }
    调用
    
    
  • 相关阅读:
    4.qml-ECMAScript(Array对象、Math对象)
    3.qml-ECMAScript_03(Object基类对象、String对象)
    2.qml-ECMAScript_02(原始值类型、通用转换方法)
    ORA-00001: 违反唯一约束条件(SOLEX.SYS_C0012537) --解决方法
    macOS 系统打开和退出文件夹(cd命令)
    macOS 系统下node安装和环境配置(通过node官网)
    macOS 系统报错:zsh:command not found :npm
    macOS 系统更新node老是不成功
    macOS 系统上升级 Python
    maxOS 系统更新node版本
  • 原文地址:https://www.cnblogs.com/huangzhen22/p/4785654.html
Copyright © 2011-2022 走看看