.NET操作注册表
最近在写一个安装程序,安装完成后需要往注册表里写入版本信息和安装路径信息,方便下次自动更新的时候找到版本和安装路径。
我参考的是文章(感谢哈):.net操作注册表
但是还是踩了个坑,所以重新梳理了本文,避免大家犯了跟我一样的错误
//c#修改注册表,需要引用Microsoft.Win32命名空间using Microsoft.Win32;namespace YourNamespace{ public class RegistryHelper { //注册表: //路径: // 32位注册表编辑器:HKEY_LOCAL_MACHINE\SOFTWARE\YourCompanyName\%YourSoftwareName%\%Key% // 64位注册表编辑器:HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\YourCompanyName\%YourSoftwareName%\%Key% //%YourSoftwareName% 代表你公司每个软件的名称 //%Key%代表某个项的Key, //每个系统的Key包含:Version、InstallPath //注册表项:"SOFTWARE"private const string _softwareKey = "SOFTWARE";//你公司的名字,我们假设你公司名下有多款软件 private const string _yourCompanyNameKey = "YourCompanyName";//软件名private const string _yourSoftwareNameKey = "YourSoftwareName"; /// /// 从注册表中读取【你的软件】的注册表键值 /// /// 注册表项 /// public static string GetRegistData(string key) { string registData; RegistryKey systemDir = GetSystemDirRegistryKey(); registData = systemDir.GetValue(key).ToString(); return registData; } /// /// 向注册表中写数据 /// /// /// 路径: /// 32位注册表编辑器:HKEY_LOCAL_MACHINE\SOFTWARE\YourCompanyName\%YourSoftwareName%\%Key% /// 64位注册表编辑器:HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\YourCompanyName\%YourSoftwareName%\%Key% /// public static void WriteToRegedit(string key, string value) { RegistryKey systemDir = GetSystemDirRegistryKey(); systemDir.SetValue(key, value); } /// /// 删除注册表中指定的注册表项 /// /// /// 路径: /// 32位注册表编辑器:HKEY_LOCAL_MACHINE\SOFTWARE\YourCompanyName\%YourSoftwareName%\%Key% /// 64位注册表编辑器:HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\YourCompanyName\%YourSoftwareName%\%Key% /// public static void DeleteRegist(string key) { RegistryKey systemDir = GetSystemDirRegistryKey(); string[] allKeys = systemDir.GetSubKeyNames(); foreach (string aimKey in allKeys) { if (aimKey == key) systemDir.DeleteSubKeyTree(key); } } /// /// 判断指定注册表项是否存在 /// /// /// 路径: /// 32位注册表编辑器:HKEY_LOCAL_MACHINE\SOFTWARE\YourCompanyName\%YourSoftwareName%\%Key% /// 64位注册表编辑器:HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\YourCompanyName\%YourSoftwareName%\%Key% /// public static bool IsRegeditExit(string key) { bool _exit = false; RegistryKey systemDir = GetSystemDirRegistryKey(); string[] allKeys = systemDir.GetSubKeyNames(); foreach (string keyName in allKeys) { if (keyName == key) { _exit = true; return _exit; } } return _exit; } #region 内部工具函数 /// /// 从注册表中获取当前系统所在文件夹 /// /// /// 路径: /// 32位注册表编辑器:HKEY_LOCAL_MACHINE\SOFTWARE\YourCompanyName\%YourSoftwareName%\%Key% /// 64位注册表编辑器:HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\YourCompanyName\%YourSoftwareName%\%Key% /// private static RegistryKey GetSystemDirRegistryKey() { RegistryKey localMachineDir = Registry.LocalMachine; if (localMachineDir == null) { //读不到localMachine 直接返回 return null; } RegistryKey softwareDir = localMachineDir.OpenSubKey(_softwareKey, true); if (softwareDir == null) { //未找到SOFTWARE文件夹,直接创建 softwareDir = localMachineDir.CreateSubKey(_softwareKey); } RegistryKey YourCompanyNameDir = softwareDir.OpenSubKey(_yourCompanyNameKey, true); if (YourCompanyNameDir == null) { //未找到YourCompanyNameDir文件夹,直接创建 YourCompanyNameDir = softwareDir.CreateSubKey(_yourCompanyNameKey); } RegistryKey yourSoftwareDir = YourCompanyNameDir.OpenSubKey(_yourSoftwareNameKey, true); if (yourSoftwareDir == null) { //未找到yourSoftwareDir文件夹,直接创建 yourSoftwareDir = YourCompanyNameDir.CreateSubKey(_yourSoftwareNameKey); } return yourSoftwareDir; } #endregion }}