C# 我的注册表操作类

  1 using System;

  2 using System.Collections.Generic;

  3 using System.Text;

  4 

  5 using Microsoft.Win32;

  6 using System.Collections;

  7 

  8 namespace War3Screen

  9 {

 10     /// <summary>

 11     /// 注册表操作类--kongfl888 2013

 12     /// 

 13     /// </summary>

 14     class RegOperator

 15     {

 16         

 17         /// <summary>

 18         /// 获取某键下的某键值

 19         /// </summary>

 20         /// <param name="_registrykey">注册表基项如 Registry.CurrentUser</param>

 21         /// <param name="mainKeystr">项的注册表键path</param>

 22         /// <param name="name"></param>

 23         /// <returns></returns>

 24         public static string Getkey(RegistryKey _registrykey, string mainKeystr,string name)

 25         {

 26             string valueStr = string.Empty;

 27 

 28             try

 29             {

 30                 

 31                 RegistryKey regKey = _registrykey;

 32                 RegistryKey mainKey = regKey.OpenSubKey(mainKeystr);

 33 

 34                 valueStr = mainKey.GetValue(name).ToString();

 35             }

 36             catch { }

 37             return valueStr;            

 38 

 39         }

 40                        

 41 

 42         /// <summary>

 43         /// 设置和修改某键值

 44         /// </summary>

 45         /// <param name="_registrykey">注册表基项如 Registry.CurrentUser</param>

 46         /// <param name="mainKeystr">项的注册表键path</param>

 47         /// <param name="name">项名</param>

 48         /// <param name="valueStr">项值</param>

 49         /// <returns></returns>

 50         public static bool Setkey(RegistryKey _registrykey, string mainKeystr, string name, string valueStr)

 51         {

 52             bool sc = false;

 53             try

 54             {

 55                 RegistryKey regKey = _registrykey;

 56                 RegistryKey mainKey = regKey.OpenSubKey(mainKeystr, true);

 57                 mainKey.SetValue(name, valueStr, RegistryValueKind.DWord);

 58 

 59                 sc = true;

 60             }

 61             catch { sc = false; }

 62             return sc;

 63         }

 64 

 65 

 66         /// <summary>

 67         /// 新建子键

 68         /// </summary>

 69         /// <param name="regKey">注册表基项如 Registry.CurrentUser</param>

 70         /// <param name="SubKeyPath">子键路径</param>

 71         /// <param name="SubKey">子键名</param>

 72         /// <param name="keyValue">子键值</param>

 73         public static bool Createkey(RegistryKey regKey, string SubKeyPath, string SubKey, string valueName, string keyValue, RegistryValueKind valueKind)

 74         {

 75             try

 76             {

 77                 RegistryKey optionKey = regKey.OpenSubKey(SubKeyPath, true);

 78                 //添加子键和值

 79                 RegistryKey subkey = optionKey.CreateSubKey(SubKey);

 80                 subkey.SetValue(valueName, keyValue, valueKind);

 81                 return true;

 82             }

 83             catch

 84             {

 85                 return false;

 86                 throw;

 87             }

 88 

 89         }

 90 

 91 

 92         /// <summary>

 93         /// 重载Createkey

 94         /// </summary>

 95         /// <param name="regKey"></param>

 96         /// <param name="SubKeyPath"></param>

 97         /// <param name="SubKey"></param>

 98         public static bool Createkey(RegistryKey regKey, string SubKeyPath, string SubKey)

 99         {

100             try

101             {

102                 RegistryKey optionKey = regKey.OpenSubKey(SubKeyPath, true);

103                 optionKey.CreateSubKey(SubKey);

104                 return true;

105             }

106             catch

107             {

108                 return false;

109                 throw;

110 

111             }

112 

113         }

114 

115 

116         /// <summary>

117         /// 删除子键

118         /// </summary>

119         /// <param name="regKey">注册表基项如 Registry.CurrentUser</param>

120         /// <param name="SubKeyPath">子键路径</param>

121         /// <param name="SubKey">子键名</param>

122         public static bool DelKey(RegistryKey regKey, string SubKeyPath, string SubKey)

123         {

124             try

125             {

126                 //打开注册表

127 

128                 RegistryKey optionKey = regKey.OpenSubKey(SubKeyPath, true);

129 

130                 string[] subKeys = optionKey.GetSubKeyNames();

131 

132                 foreach (string akey in subKeys)

133                 {

134                     if (akey == SubKey)

135                     {

136                         optionKey.DeleteSubKeyTree(SubKey);

137                         return true;

138                     }

139                 }

140                 return false;

141 

142             }

143             catch

144             {

145                 return false;

146                 throw;

147             }

148         }

149 

150 

151 

152 

153     }

154 }

 

你可能感兴趣的:(注册表)