• 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);
                    }
                }
            }

  • 相关阅读:
    redis.conf配置详细解析
    laravel框架的注入
    10 个免费高清图片素材下载网站。#免版权# #设计# #图片处理#
    本地Git连接GitLab(服务器)远程仓库
    基于Docker的Mysql主从复制
    解决git本地代码推服务器每次都要输入用户名和密码的问题
    Laravel上传文件(单文件,多文件)
    php的精度计算问题(bcadd和bcsub)
    POJ 1573 Robot Motion(简单模拟)
    POJ 2996 Help Me with the Game(模拟)
  • 原文地址:https://www.cnblogs.com/scottckt/p/800779.html
Copyright © 2020-2023  润新知