.NET Framework SDK 提供兩個核心方法操作登錄:
CreateSubKey(string key):建立子鍵(亦可用於建立主鍵層級)SetValue(string name, string keyvalue):設定鍵值;若鍵值存在則修改,不存在則新增HKEY_LOCAL_MACHINE\HARDWARE 下建立 aaa 子鍵與 bbb 鍵值,供重命名測試RegistryKey hklm = Registry.LocalMachine;
RegistryKey hw = hklm.OpenSubKey("HARDWARE", true);
RegistryKey ddd = hw.CreateSubKey("ddd");
ddd.SetValue("www", "1234");
RegistryKey main = hw.CreateSubKey("main");
RegistryKey sub = main.CreateSubKey("sub");
sub.SetValue("value", "1234");
RegistryKey aaa = hw.OpenSubKey("aaa", true);
aaa.SetValue("bbb", "abcd");
注意:.NET Framework SDK 當時未提供刪除/重命名鍵的方法,SetValue 為風險相對較小的寫入操作。