zoukankan      html  css  js  c++  java
  • C#操作注冊表學習

        今天偶然看見一篇有修改注冊表的文章,心中便想C#是否可以修改注冊表,於是自己便從網上找了一些文章學習。
    雖然學的很淺,但自己又進步了!

    首先了解下C#操作注冊表的類
    (1).Registry类:此类主要封装了七个公有的静态域,而这些静态域分别代表这视窗注册表中的七个基本的主键,具体如下所示:
    Registry.ClassesRoot     对应于HKEY_CLASSES_ROOT主键

    Registry.CurrentUser 对应于HKEY_CURRENT_USER主键

    Registry.LocalMachine 对应于 HKEY_LOCAL_MACHINE主键

    Registry.User 对应于 HKEY_USER主键

    Registry.CurrentConfig 对应于HEKY_CURRENT_CONFIG主键

    Registry.DynDa 对应于HKEY_DYN_DATA主键

    Registry.PerformanceData 对应于HKEY_PERFORMANCE_DATA主键
    (2).RegistryKey类:此类中主要封装了对视窗系统注册表的基本操作。在程序设计中,首先通过Registry类找到注册表中的基本主键,然后通过RegistryKey类,来找其下面的子键和处理具体的操作的。

    代碼:
           using Microsoft.Win32;

            /// <summary>
            
    ///得到注冊表中的值
            
    /// </summary>
            
    /// <returns></returns>
            private string GetRegistData( )
            {
                
    string registData;
                RegistryKey hkml 
    = Registry.LocalMachine;
                RegistryKey RootFolder 
    = Registry.LocalMachine;
                RegistryKey system 
    = RootFolder.OpenSubKey("system",true);
                RegistryKey CurrentControlSet 
    = system.OpenSubKey("CurrentControlSet",true);
                RegistryKey Services 
    = CurrentControlSet.OpenSubKey("Services",true);
                RegistryKey Tcpip 
    = Services.OpenSubKey("Tcpip",true);
                RegistryKey Parameters 
    = Tcpip.OpenSubKey("Parameters",true);
                RegistryKey NetCardID 
    = Parameters.OpenSubKey("Winsock",true);    
                registData 
    = NetCardID.GetValue("HelperDllName""null").ToString();
                
    return registData;
            }
            
    /// <summary>
            
    /// 創建注冊表值
            
    /// 本例為創建一個XX的子目錄,其創建一鍵為XXX,值為test
            
    /// </summary>
            private void CreateRegistData()
            {            
                RegistryKey hkml 
    = Registry.LocalMachine;
                RegistryKey RootFolder 
    = Registry.LocalMachine;
                RegistryKey system 
    = RootFolder.OpenSubKey("system"true);
                RegistryKey CurrentControlSet 
    = system.OpenSubKey("CurrentControlSet"true);
                RegistryKey Services 
    = CurrentControlSet.OpenSubKey("Services"true);
                RegistryKey Tcpip 
    = Services.OpenSubKey("Tcpip"true);
                RegistryKey Parameters 
    = Tcpip.OpenSubKey("Parameters"true);
                RegistryKey NetCardID 
    = Parameters.OpenSubKey("Winsock"true);
                RegistryKey xxx
    = NetCardID.CreateSubKey("xxx");
                
    //384為十進制,Dword為16進制,系統將自動進行轉換,最終注冊表存入的值為180,180為384的16進制
                xxx.SetValue("xxx",384,RegistryValueKind.DWord);    
            }

            
    /// <summary>
            
    /// 刪除指的鍵
            
    /// </summary>
            private void DeleteValueRegistData()
            {
                RegistryKey hkml 
    = Registry.LocalMachine;
                RegistryKey RootFolder 
    = Registry.LocalMachine;
                RegistryKey system 
    = RootFolder.OpenSubKey("system"true);
                RegistryKey CurrentControlSet 
    = system.OpenSubKey("CurrentControlSet"true);
                RegistryKey Services 
    = CurrentControlSet.OpenSubKey("Services"true);
                RegistryKey Tcpip 
    = Services.OpenSubKey("Tcpip"true);
                RegistryKey Parameters 
    = Tcpip.OpenSubKey("Parameters"true);
                RegistryKey NetCardID 
    = Parameters.OpenSubKey("Winsock"true);
                RegistryKey xxx 
    = NetCardID.OpenSubKey("xxx"true);
                
    string[] SubValue;
                SubValue 
    = xxx.GetValueNames();
                
    foreach (string sSubValue in SubValue)
                {
                    
    if (sSubValue=="xxx")
                    {
                        xxx.DeleteValue(sSubValue);
                    }
                }
            }
            
    /// <summary>
            
    /// 刪除子目錄
            
    /// </summary>
            private void DeleteSubRegistData()
            {
                RegistryKey hkml 
    = Registry.LocalMachine;
                RegistryKey RootFolder 
    = Registry.LocalMachine;
                RegistryKey system 
    = RootFolder.OpenSubKey("system"true);
                RegistryKey CurrentControlSet 
    = system.OpenSubKey("CurrentControlSet"true);
                RegistryKey Services 
    = CurrentControlSet.OpenSubKey("Services"true);
                RegistryKey Tcpip 
    = Services.OpenSubKey("Tcpip"true);
                RegistryKey Parameters 
    = Tcpip.OpenSubKey("Parameters"true);
                RegistryKey NetCardID 
    = Parameters.OpenSubKey("Winsock"true);
                
    string[] SubValue;
                SubValue 
    = NetCardID.GetSubKeyNames();
                
    foreach (string sSubValue in SubValue)
                {               
                    
    if (sSubValue == "xxx")
                    {                    
                        NetCardID.DeleteSubKey (sSubValue);
                    }
                }
            }

  • 相关阅读:
    深度学习3--caffe的安装(only CPU)
    深度学习2--安装opencv3.1
    深度学习1--ubuntu14.04+win10双系统
    java回调机制——基本理解
    mysql修改默认端口号后从windows命令行登录
    UTF-8编码与GBK编码下的字符长度
    代码质量的几点新思考
    软件工程质量之“工程元数据”
    Junit概述
    maven+springmvc项目启动时,request mapping not found……
  • 原文地址:https://www.cnblogs.com/scottckt/p/800779.html
Copyright © 2011-2022 走看看