WPF开发随笔收录-操作注册表
一、前言
在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,防止程序出现异常奔溃;