C#操作注冊表學習

來源:互聯網
上載者:User
    今天偶然看見一篇有修改注冊表的文章,心中便想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);
                }
            }
        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.