tft每日頭條

 > 生活

 > wpf方法介紹總結

wpf方法介紹總結

生活 更新时间:2024-12-19 03:47:03
一、前言

在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,防止程序出現異常崩潰;

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved