C#操作注册表增删改查及关机能保存问题
重啟電腦創(chuàng)建的注冊(cè)表鍵值丟失原因
REG_OPTION_VOLATILE? 這個(gè)參數(shù)的意思是創(chuàng)建的注冊(cè)表鍵值都位于內(nèi)存中,不會(huì)保存到相應(yīng)的注冊(cè)表文件中。
英文如下:
All registry keys are created as volatile, and the information is stored in memory and is not preserved when the corresponding registry hive is unloaded. For HKEY_LOCAL_MACHINE, this occurs when the OS is shut down. TheRegSaveKey?function does not save volatile registry keys. This flag is ignored for keys that already exist.
所以,重啟后這些鍵值當(dāng)然就沒有了。
解決辦法
很簡(jiǎn)單,使用REG_OPTION_NON_VOLATILE?即可
RegCreateKeyEx(HKEY_CURRENT_USER, DemoRegKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS , NULL , &hKey, NULL);REG_OPTION_VOLATILE可以用在測(cè)試上。一重啟,之前創(chuàng)建的鍵值都沒了。
而若想重啟之后注冊(cè)表鍵值也仍然保留的話就用REG_OPTION_NON_VOLATILE。
?REG_OPTION_NON_VOLATILE:
該宏是RegCreateKeyEx函數(shù)的參數(shù)可選項(xiàng)。
LONG RegCreateKeyEx( HKEY hKey,
LPCWSTR lpSubKey,
?DWORD Reserved,
LPWSTR lpClass,
DWORD dwOptions,
REGSAM samDesired,
?LPSECURITY_ATTRIBUTES lpSecurityAttributes,
?PHKEY phkResult,
LPDWORD lpdwDisposition );
看看MSDN對(duì)它的說明:
Default setting. All registry keys are created as non-volatile and the information stored in memory is preserved when the OS is restarted. The RegSaveKey function saves keys that are non-volatile.
翻譯:默認(rèn)設(shè)置。 所有注冊(cè)表項(xiàng) 創(chuàng)建 作為非易失性 和 操作系統(tǒng) 重新啟動(dòng) 時(shí)保留 在內(nèi)存 中存儲(chǔ)的信息 。 RegSaveKey 功能 鍵 是 非易失性 保存 。
意思是說如果參數(shù)?dwOptions?選擇REG_OPTION_NON_VOLATILE,則通過RegCreateKeyEx創(chuàng)建的注冊(cè)表是永久的,不會(huì)因?yàn)橛?jì)算機(jī)的重啟而消失。相反參數(shù)選擇REG_OPTION_VOLATILE,則表明創(chuàng)建的注冊(cè)表是臨時(shí)的,計(jì)算機(jī)重啟后該注冊(cè)表不存在。
dwOptions
[in] Registry key options. The following table shows the possible values for this parameter.
| REG_OPTION_NON_VOLATILE | Default setting. All registry keys are created as non-volatile and the information stored in memory is preserved when the OS is restarted. The?RegSaveKey?function saves keys that are non-volatile. |
| REG_OPTION_VOLATILE | All registry keys are created as volatile, and the information is stored in memory and is not preserved when the corresponding registry hive is unloaded. For HKEY_LOCAL_MACHINE, this occurs when the OS is shut down. The?RegSaveKey?function does not save volatile registry keys. This flag is ignored for keys that already exist. |
一.注冊(cè)表巢
? ? ?在注冊(cè)表中,最上面的節(jié)點(diǎn)是注冊(cè)表巢(registry hive)。
? ? ?HKEY_CLASSES_ROOT(HKCR) ? ?包含系統(tǒng)文件類型的細(xì)節(jié),以及應(yīng)用程序可以打開的文件類型,它還包含所有COM組件的注冊(cè)信息。
? ? ?HKEY_CURRENT_USER(HKCU) ? ?包含用戶目前登陸的機(jī)器的用戶配置,包括桌面設(shè)置、環(huán)境變量、網(wǎng)絡(luò)和打印機(jī)連接和其他定義用戶操作環(huán)境的變量。
? ? ?HKEY_LOCAL_MACHINE(HKLM) ? ?是一個(gè)很大的巢,其中包含所有安裝到機(jī)器上的軟件和硬件的信息。
? ? ?HKEY_USERS(HKUSR) ? ? ? ? ? ? ? ?包含所有用戶的用戶配置。
? ? ?HKEY_CURRENT_CONFIG(HKCF) ?包含機(jī)器上硬件的信息。
二.注冊(cè)表類及常用屬性和函數(shù)
using Microsoft.Win32; 這個(gè)命名空間包含了注冊(cè)表相關(guān)的類。Registry類、RegistryKey類。 Registry類封裝了注冊(cè)表的七個(gè)基本主鍵:| Registry.ClassesRoot | 對(duì)應(yīng)于HKEY_CLASSES_ROOT主鍵 |
| Registry.CurrentUser | 對(duì)應(yīng)于HKEY_CURRENT_USER主鍵 |
| Registry.LocalMachine | 對(duì)應(yīng)于 HKEY_LOCAL_MACHINE主鍵 |
| Registry.User | 對(duì)應(yīng)于 HKEY_USER主鍵 |
| Registry.CurrentConfig | 對(duì)應(yīng)于HEKY_CURRENT_CONFIG主鍵 |
| Registry.DynDa | 對(duì)應(yīng)于HKEY_DYN_DATA主鍵 |
| Registry.PerformanceData | 對(duì)應(yīng)于HKEY_PERFORMANCE_DATA主鍵 |
| Name | 鍵的名稱(只讀) |
| SubKeyCount | 鍵的子鍵個(gè)數(shù) |
| ValueCount | 鍵包含的值的個(gè)數(shù) |
| Close() | 關(guān)閉鍵 |
| CreateSubKey() | 創(chuàng)建給定名稱的子鍵 |
| DeleteSubKey() | 刪除指定的子鍵 |
| DeleteSubKeyTree() | 遞歸刪除子鍵及其所有的子鍵 |
| DeleteValue() | 從鍵中刪除一個(gè)指定的值 |
| GetAccessControl() | 返回指定注冊(cè)表鍵的訪問控制表 |
| GetSubKeyNames() | 返回包含子鍵名稱的字符串?dāng)?shù)組 |
| GetValue() | 返回指定的值 |
| GetValueKind() | 返回指定的值,檢索其注冊(cè)表數(shù)據(jù)類型 |
| GetValueNames() | 返回一個(gè)包含所有鍵值名稱的字符串?dāng)?shù)組 |
| OpenSubKey() | 返回表示給定子鍵的RegistryKey實(shí)例引用 |
| SetAccessControl() | 把訪問控制表(ACL)應(yīng)用于指定的注冊(cè)表鍵 |
| SetValue() | 設(shè)置指定的值 |
? ? ? ?2,打開
//使用OpenSubKey()打開項(xiàng),獲得RegistryKey對(duì)象,當(dāng)路徑不存在時(shí),為Null。第二個(gè)參數(shù)為true,表示可寫,可讀,可刪;省略時(shí)只能讀。 RegistryKey hklm = Registry.LocalMachine; RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\test",true); hklm.Close(); hkSoftWare.Close();? ? ? 3,刪除
//主要用到了DeleteSubKey(),刪除test項(xiàng) RegistryKey hklm = Registry.LocalMachine; hklm.DeleteSubKey(@"SOFTWARE\test", true); //為true時(shí),刪除的注冊(cè)表不存在時(shí)拋出異常;當(dāng)為false時(shí)不拋出異常。 hklm.Close();四、注冊(cè)表鍵值的創(chuàng)建、打開和刪除
? ? ? 1,創(chuàng)建
//主要用到了SetValue(),表示在test下創(chuàng)建名稱為Name,值為RegistryTest的鍵值。第三個(gè)參數(shù)表示鍵值類型,省略時(shí),默認(rèn)為字符串 RegistryKey hklm = Registry.LocalMachine; RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\test",true); hkSoftWare.SetValue("Name", "RegistryTest", RegistryValueKind.String); hklm.Close(); hkSoftWare.Close();? ? ?2,打開
//主要用到了GetValue(),獲得名稱為"Name"的鍵值 RegistryKey hklm = Registry.LocalMachine; RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\test", true); string sValue = hkSoftWare.GetValue("Name").ToString(); hklm.Close(); hkSoftWare.Close();? ? ?3,刪除
//主要用到了DeleteValue(),表示刪除名稱為"Name"的鍵值,第二個(gè)參數(shù)表示是否拋出異常 RegistryKey hklm = Registry.LocalMachine; RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\test", true); hkSoftWare.DeleteValue("Name", true); hklm.Close(); hkSoftWare.Close();五、判斷注冊(cè)表項(xiàng)、注冊(cè)表鍵值是否存在
//判斷注冊(cè)表項(xiàng)是否存在private bool IsRegistryKeyExist(string sKeyName){string[] sKeyNameColl;RegistryKey hklm = Registry.LocalMachine;RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE");sKeyNameColl = hkSoftWare.GetSubKeyNames(); //獲取SOFTWARE下所有的子項(xiàng)foreach (string sName in sKeyNameColl){if (sName == sKeyName){hklm.Close();hkSoftWare.Close();return true;}}hklm.Close();hkSoftWare.Close();return false;}//判斷鍵值是否存在private bool IsRegistryValueNameExist(string sValueName){string[] sValueNameColl;RegistryKey hklm = Registry.LocalMachine;RegistryKey hkTest = hklm.OpenSubKey(@"SOFTWARE\test");sValueNameColl = hkTest.GetValueNames(); //獲取test下所有鍵值的名稱foreach (string sName in sValueNameColl){if (sName == sValueName){hklm.Close();hkTest.Close();return true;}}hklm.Close();hkTest.Close();return false;}六、程序自啟動(dòng)程序
//開啟程序自啟動(dòng)string path = Application.ExecutablePath;RegistryKey rk = Registry.LocalMachine;RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");rk2.SetValue("JcShutdown", path);rk2.Close();rk.Close();//關(guān)閉程序自啟動(dòng)string path = Application.ExecutablePath;RegistryKey rk = Registry.LocalMachine;RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");rk2.DeleteValue("JcShutdown", false);rk2.Close();rk.Close();總結(jié)
以上是生活随笔為你收集整理的C#操作注册表增删改查及关机能保存问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Docker : Dockerfile
- 下一篇: c#中Show和Showdialog的区