C# 操作註冊表WindowsRegistry
阿新 • • 發佈:2017-08-15
word win32 tostring pub .get als brush mach 新建
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Win32; namespace BugsBox.Application.Core { public class WindowsRegistry { const string REGISTRY_ITEM_PATH = "software\\pharmacyinst"; /// <summary> /// 創建註冊表項 /// </summary> /// <param name="registryItemPath"></param> public static void CreateRegistry(string registryItemPath = REGISTRY_ITEM_PATH) { RegistryKey key = Registry.LocalMachine; RegistryKey software = key.CreateSubKey(registryItemPath); //在HKEY_LOCAL_MACHINE\SOFTWARE下新建名為test的註冊表項。如果已經存在則不影響! } /// <summary> /// 獲取註冊表值 /// </summary> /// <returns></returns> public static string GetRegistryValue(string key, string registryItemPath = REGISTRY_ITEM_PATH) { string info = ""; RegistryKey Key; Key = Registry.LocalMachine; var myreg = Key.OpenSubKey(registryItemPath); info = myreg.GetValue(key).ToString(); myreg.Close(); return info; } /// <summary> /// 修改註冊表項值 /// </summary> /// <param name="subKey"></param> /// <param name="val"></param> /// <param name="registryItemPath"></param> public static void SetRegistryValue(string subKey, string val, string registryItemPath = REGISTRY_ITEM_PATH) { using (RegistryKey key = Registry.LocalMachine) { RegistryKey software = key.OpenSubKey(registryItemPath, true); //該項必須已存在 software.SetValue(subKey, val); //在HKEY_LOCAL_MACHINE\SOFTWARE\test下創建一個名為“test”,值為“博客園”的鍵值。如果該鍵值原本已經存在,則會修改替換原來的鍵值,如果不存在則是創建該鍵值。 // 註意:SetValue()還有第三個參數,主要是用於設置鍵值的類型,如:字符串,二進制,Dword等等~~默認是字符串。如: // software.SetValue("test", "0", RegistryValueKind.DWord); //二進制信息 key.Close(); } } /// <summary> /// 刪除註冊表項 /// </summary> public static void DeleteRegistry(string registryItemPath = REGISTRY_ITEM_PATH) { RegistryKey key = Registry.LocalMachine; key.DeleteSubKey(registryItemPath, true); //該方法無返回值,直接調用即可 key.Close(); } /// <summary> /// 刪除註冊表值 /// </summary> public static void DeleteRegistryValue(string subKey, string registryItemPath = REGISTRY_ITEM_PATH) { RegistryKey delKey = Registry.LocalMachine.OpenSubKey(registryItemPath, true); delKey.DeleteValue(subKey); delKey.Close(); } /// <summary> /// 判斷註冊表項是否存在 /// </summary> /// <returns></returns> public static bool IsRegeditItemExist() { string[] subkeyNames; RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey("SOFTWARE"); //RegistryKey software = hkml.OpenSubKey("SOFTWARE", true); subkeyNames = software.GetSubKeyNames(); //取得該項下所有子項的名稱的序列,並傳遞給預定的數組中 foreach (string keyName in subkeyNames) //遍歷整個數組 { if (keyName == "test") //判斷子項的名稱 { hkml.Close(); return true; } } hkml.Close(); return false; } /// <summary> /// 判斷鍵值是否存在 /// </summary> /// <returns></returns> public static bool IsRegeditKeyExist(string subKey, string registryItemPath = REGISTRY_ITEM_PATH) { string[] subkeyNames; RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey(registryItemPath); //RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test", true); subkeyNames = software.GetValueNames(); //取得該項下所有鍵值的名稱的序列,並傳遞給預定的數組中 foreach (string keyName in subkeyNames) { if (keyName == subKey) //判斷鍵值的名稱 { hkml.Close(); return true; } } hkml.Close(); return false; } } }
C# 操作註冊表WindowsRegistry