一、前言 在windows平臺軟體開發過程中,註冊表的操作是經常會遇到的一個場景。今天記錄一下在操作註冊表時遇到的一些坑; 二、正文 1、操作註冊表,於是直接從網上找了一段代碼來用 /// <summary> /// 讀取註冊表 /// </summary> /// <param name="nam ...
一、前言
在windows平臺軟體開發過程中,註冊表的操作是經常會遇到的一個場景。今天記錄一下在操作註冊表時遇到的一些坑;
二、正文
1、操作註冊表,於是直接從網上找了一段代碼來用
/// <summary> /// 讀取註冊表 /// </summary> /// <param name="name"></param> /// <returns></returns> public static string GetRegistData(string name) { string registData; RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey("SOFTWARE", true); RegistryKey aimdir = software.OpenSubKey("XXX", true); registData = aimdir.GetValue(name).ToString(); return registData; } /// <summary> /// 寫入註冊表 /// </summary> /// <param name="name"></param> /// <param name="tovalue"></param> public static void WriteRegedit(string name, string tovalue) { RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey("SOFTWARE", true); RegistryKey aimdir = software.CreateSubKey("XXX"); aimdir.SetValue(name, tovalue); } /// <summary> /// 刪除註冊表 /// </summary> /// <param name="name"></param> public static void DeleteRegist(string name) { string[] aimnames; RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey("SOFTWARE", true); RegistryKey aimdir = software.OpenSubKey("XXX", true); aimnames = aimdir.GetSubKeyNames(); foreach (string aimKey in aimnames) { if (aimKey == name) aimdir.DeleteSubKeyTree(name); } }
2、但在使用過程中,發現通過這種方式寫的註冊表值雖然能讀取出來,但是在電腦上打開註冊表工具,卻無法查看到對應自己寫入的註冊表值,翻閱資料後發現這樣寫有問題,還需要判斷一下電腦是32位的還是64位的,需要做一下修改;參考下麵修改後的代碼,先查出電腦對應的位數,再去操作對應指定位數的註冊表;
/// <summary> /// 讀取註冊表 /// </summary> /// <param name="name"></param> /// <returns></returns> public static string GetRegistData(string name) { string registData; RegistryView useRegistryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32; RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, useRegistryView); RegistryKey software = hklm.OpenSubKey("SOFTWARE", true); RegistryKey aimdir = software.OpenSubKey("XXX", true); registData = aimdir.GetValue(name).ToString(); return registData; } /// <summary> /// 寫入註冊表 /// </summary> /// <param name="name"></param> /// <param name="tovalue"></param> public static void WriteRegedit(string name, string tovalue) { RegistryView useRegistryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32; RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, useRegistryView); RegistryKey software = hklm.OpenSubKey("SOFTWARE", true); RegistryKey aimdir = software.CreateSubKey("XXX"); aimdir.SetValue(name, tovalue); } /// <summary> /// 刪除註冊表 /// </summary> /// <param name="name"></param> public static void DeleteRegist(string name) { string[] aimnames; RegistryView useRegistryView = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32; RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, useRegistryView); RegistryKey software = hklm.OpenSubKey("SOFTWARE", true); RegistryKey aimdir = software.OpenSubKey("XXX", true); aimnames = aimdir.GetSubKeyNames(); foreach (string aimKey in aimnames) { if (aimKey == name) aimdir.DeleteSubKeyTree(name); } }
3、由於註冊表的操作涉及到管理員許可權,所以上面的幾個方法里最好加上try,防止程式出現異常奔潰;