【1】註冊表是什麼? 百度百科 https://baike.baidu.com/item/%E6%B3%A8%E5%86%8C%E8%A1%A8/101856?fr=aladdin 【2】怎麼打開註冊表? Windows+R >>>>>> regedit 【3】C# 源碼 此博文為原創,轉載請註明出 ...
【1】註冊表是什麼?
百度百科 https://baike.baidu.com/item/%E6%B3%A8%E5%86%8C%E8%A1%A8/101856?fr=aladdin
【2】怎麼打開註冊表?
Windows+R >>>>>> regedit
【3】C# 源碼
1 /// <summary> 2 /// 註冊表 CURD 3 /// 在LocalMachine 目錄文件下 4 /// </summary> 5 public class RegeditUtils 6 { 7 /// <summary> 8 /// 註冊信息節點是否存在 9 /// </summary> 10 /// <param name="key">"SOFTWARE\\Microsoft"格式</param> 11 /// <returns></returns> 12 public static bool IsExsits(string key) 13 { 14 var indicator = true; 15 if (Registry.LocalMachine.OpenSubKey(key) == null) 16 { 17 indicator = false; 18 } 19 return indicator; 20 } 21 /// <summary> 22 /// 創建註冊信息 23 /// </summary> 24 /// <param name="key">"SOFTWARE\\Microsoft"格式</param> 25 public static void Create(string key) 26 { 27 Registry.LocalMachine.CreateSubKey(key); 28 } 29 /// <summary> 30 /// 更新註冊表欄位值 31 /// </summary> 32 /// <param name="rsg">傳入註冊表節點</param> 33 /// <param name="name">節點內欄位名稱</param> 34 /// <param name="value">需要設置的值</param> 35 public static void Update(RegistryKey rsg,string name,string value) 36 { 37 if (rsg!=null) 38 { 39 if (!string.IsNullOrEmpty(name)) 40 { 41 rsg.SetValue(name, value); //寫入 42 } 43 rsg.Close(); //關閉 44 } 45 } 46 /// <summary> 47 /// 獲取註冊表節點實體 48 /// </summary> 49 /// <param name="key">"SOFTWARE\\Microsoft"格式</param> 50 /// <returns></returns> 51 public static RegistryKey Read(string key) 52 { 53 RegistryKey rsg = Registry.LocalMachine.OpenSubKey(key, true); 54 return rsg; 55 } 56 57 /// <summary> 58 /// 獲取註冊表節點實體內某個屬性的值 59 /// </summary> 60 /// <param name="key">"SOFTWARE\\Microsoft"格式</param> 61 /// <param name="prop">傳入屬性的鍵值</param> 62 /// <returns></returns> 63 public static string ReadPropValue(string key,string prop) 64 { 65 var res = ""; 66 RegistryKey rsg = Registry.LocalMachine.OpenSubKey(key, true); 67 if (rsg!=null) 68 { 69 var obj = rsg.GetValue(prop); 70 if (obj!=null) 71 { 72 res = obj.ToString(); 73 } 74 } 75 return res; 76 } 77 /// <summary> 78 /// 刪除註冊表節點 79 /// 80 /// </summary> 81 /// <param name="key">"SOFTWARE\\Microsoft"格式</param> 82 public static void Delete(string key) 83 { 84 if (IsExsits(key)) 85 { 86 Registry.LocalMachine.DeleteSubKey(key); 87 } 88 } 89 90 /// <summary> 91 /// 創建註冊表節點,並對相應欄位賦值 92 /// </summary> 93 /// <param name="key">節點</param> 94 /// <param name="name">屬性名稱</param> 95 /// <param name="value">值</param> 96 public static void CreateRegedit(string key, string name, string value) 97 { 98 try 99 { 100 101 //判斷當前節點是否存在 102 if (!IsExsits(key)) 103 { 104 //不存在該節點,創建節點 105 Create(key); 106 } 107 //獲取該節點 108 var rsg = Read(key); 109 //該節點欄位屬性賦值 110 Update(rsg, name, value); 111 } 112 catch (Exception ex) 113 { 114 Log.Error(ex); 115 } 116 117 } 118 }
此博文為原創,轉載請註明出處!!!!!