C#操作注册表

using Microsoft.Win32;

  1 /// <summary>注册表操作相关</summary>
  2     public class Regedit
  3     {
  4         /// <summary>注册表 </summary>
  5         public static string strRkSoftware = "SOFTWARE";
  6         /// <summary> 目录 </summary>
  7         public static string strRkDir = "Winner2";
  8 
  9         /// <summary> 存放最后更新时间的注册表键名称 </summary>
 10         public static string strKey = "01EF386";
 11 
 12         /// <summary> 存放用户增值服务注册情况的注册表键名称 </summary>
 13         public static string strKeyIsUserReg = "1E29A79";
 14 
 15         /// <summary> 存放用户代理服务器的信息 </summary>
 16         public static string strKeyProxy = "Proxy";
 17 
 18         /// <summary> 存放用户基本信息 </summary>
 19         public static string UserInfo = "UserInfo";
 20 
 21         /// <summary> 读取指定名称的注册表的值 </summary>
 22         /// <param name="p_Name">指定名称</param>
 23         /// <returns></returns>
 24         public static string GetRegistData(string p_Name)
 25         {
 26             string strRegistData;
 27             RegistryKey rkLocalMachine = Registry.LocalMachine;
 28             RegistryKey rkSoftware = rkLocalMachine.OpenSubKey(strRkSoftware, true);
 29             RegistryKey rkDir = rkSoftware.OpenSubKey(strRkDir, true);
 30 
 31             try
 32             {
 33                 strRegistData = rkDir.GetValue(p_Name).ToString();
 34             }
 35             catch
 36             {
 37                 return "";
 38             }
 39             return strRegistData;
 40         }
 41 
 42         /// <summary>向注册表中写数据</summary>
 43         /// <param name="p_Name">指定名称</param>
 44         /// <param name="p_Value">指定值</param>
 45         public static void WriteRegedit(string p_Name, string p_Value)
 46         {
 47             RegistryKey rkLocalMachine = Registry.LocalMachine;
 48             RegistryKey rkSoftware = rkLocalMachine.OpenSubKey(strRkSoftware, true);
 49             RegistryKey rkDir = rkSoftware.CreateSubKey(strRkDir);
 50 
 51             rkDir.SetValue(p_Name, p_Value);
 52         }
 53 
 54         /// <summary>删除注册表中指定的注册表项</summary>
 55         /// <param name="p_Name">指定名称</param>
 56         public static void DeleteRegist(string p_Name)
 57         {
 58             string[] saAimnames;
 59             RegistryKey rkLocalMachine = Registry.LocalMachine;
 60             RegistryKey rkSoftware = rkLocalMachine.OpenSubKey(strRkSoftware, true);
 61             RegistryKey rkDir = rkSoftware.OpenSubKey(strRkDir, true);
 62             try
 63             {
 64                 saAimnames = rkDir.GetValueNames();
 65                 foreach (string strAimKey in saAimnames)
 66                 {
 67                     if (strAimKey == p_Name)
 68                         rkDir.DeleteValue(p_Name);
 69                 }
 70             }
 71             catch
 72             {
 73                 return;
 74             }
 75         }
 76 
 77         /// <summary>判断指定注册表项是否存在 </summary>
 78         /// <param name="p_Name">指定名称</param>
 79         /// <returns></returns>
 80         public static bool IsRegeditExit(string p_Name)
 81         {
 82             try
 83             {
 84                 bool blnExit = false;
 85                 string[] saSubkeyNames;
 86                 RegistryKey rkLocalMachine = Registry.LocalMachine;
 87                 RegistryKey rkSoftware = rkLocalMachine.OpenSubKey(strRkSoftware, true);
 88                 RegistryKey rkDir = rkSoftware.OpenSubKey(strRkDir, true);
 89 
 90                 saSubkeyNames = rkDir.GetValueNames();
 91                 foreach (string strKeyName in saSubkeyNames)
 92                 {
 93                     if (strKeyName == p_Name)
 94                     {
 95                         blnExit = true;
 96                         return blnExit;
 97                     }
 98                 }
 99                 return blnExit;
100             }
101             catch { return false; }
102         }
103     }
posted @ 2012-12-26 18:28  EleMMent  阅读(208)  评论(0编辑  收藏  举报