wpf方法介绍总结(WPF开发随笔收录-操作注册表)

一、前言

在windows平台软件开发过程中,注册表的操作是经常会遇到的一个场景。今天记录一下在操作注册表时遇到的一些坑;

二、正文

1、操作注册表,于是直接从网上找了一段代码来用

wpf方法介绍总结(WPF开发随笔收录-操作注册表)(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); } }

wpf方法介绍总结(WPF开发随笔收录-操作注册表)(2)

2、但在使用过程中,发现通过这种方式写的注册表值虽然能读取出来,但是在电脑上打开注册表工具,却无法查看到对应自己写入的注册表值,翻阅资料后发现这样写有问题,还需要判断一下电脑是32位的还是64位的,需要做一下修改;参考下面修改后的代码,先查出电脑对应的位数,再去操作对应指定位数的注册表;

wpf方法介绍总结(WPF开发随笔收录-操作注册表)(3)

/// <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); } }

wpf方法介绍总结(WPF开发随笔收录-操作注册表)(4)

3、由于注册表的操作涉及到管理员权限,所以上面的几个方法里最好加上try,防止程序出现异常崩溃;

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页