c# 注册表操作

 1/// <summary>
 2        /// 读取注册表值
 3        /// </summary>
 4        /// <param name="strName"></param>
 5        /// <returns></returns>

 6        public static string GetRegEditData(string strName)
 7        {
 8            try
 9            {
10                string registData; 
11                RegistryKey hkml = Registry.LocalMachine; 
12                RegistryKey software = hkml.OpenSubKey("SOFTWARE",true); 
13                RegistryKey aimdir = software.OpenSubKey("username",true); 
14                registData = aimdir.GetValue(strName).ToString(); 
15                return MD5Decrypt(registData);
16            }

17            catch(Exception ex)
18            {    
19                Log.WriteDataBase(DateTime.Now,"3",FrmMain.userName,ex.Message,"主机名:"+System.Net.Dns.GetHostName()+";IP地址:"+FrmMain.addressList[0]);
20                return "";
21            }

22        }

 1/// <summary>
 2        /// 写入注册表
 3        /// </summary>
 4        /// <param name="strName"></param>

 5        public static void SetRegEditData( string strName,string strValue)
 6        {
 7            try
 8            {
 9                RegistryKey hklm = Registry.LocalMachine;
10                RegistryKey software = hklm.OpenSubKey("SOFTWARE",true);
11                RegistryKey aimdir = software.CreateSubKey("username");
12                aimdir.SetValue(strName,MD5Encrypt(strValue));
13            }

14            catch(Exception ex)
15            {    
16                Log.WriteDataBase(DateTime.Now,"3",FrmMain.userName,ex.Message,"主机名:"+System.Net.Dns.GetHostName()+";IP地址:"+FrmMain.addressList[0]);
17            }

18        }

 1/// <summary>
 2        /// 修改注册表项
 3        /// </summary>
 4        /// <param name="strName"></param>
 5        /// <param name="strValue"></param>

 6        public static void ModifyRegEditData( string strName ,string strValue)
 7        {
 8            try
 9            {
10                RegistryKey hklm = Registry.LocalMachine;
11                RegistryKey software = hklm.OpenSubKey("SOFTWARE\\username",true);
12                software.SetValue(strName,MD5Encrypt(strValue));
13            }

14            catch(Exception ex)
15            {
16                Log.WriteDataBase(DateTime.Now,"3",FrmMain.userName,ex.Message,"主机名:"+System.Net.Dns.GetHostName()+";IP地址:"+FrmMain.addressList[0]);
17            }

18        }

 1/// <summary>
 2        /// 判断指定注册表项是否存在
 3        /// </summary>
 4        /// <param name="strName"></param>
 5        /// <returns></returns>

 6        public static bool IsExist(string strName)
 7        {
 8            try
 9            {
10                bool exit = false
11                string[] subkeyNames; 
12                RegistryKey hkml = Registry.LocalMachine; 
13                RegistryKey software = hkml.OpenSubKey("SOFTWARE",true); 
14                RegistryKey aimdir = software.OpenSubKey("username",true);
15                subkeyNames = aimdir.GetValueNames();
16                foreach(string keyName in subkeyNames) 
17                
18                    if(keyName == strName) 
19                    
20                        exit = true
21                        return exit; 
22                    }
 
23                }
 
24                return exit;
25            }

26            catch(Exception ex)
27            {
28                Log.WriteDataBase(DateTime.Now,"3",FrmMain.userName,ex.Message,"主机名:"+System.Net.Dns.GetHostName()+";IP地址:"+FrmMain.addressList[0]);
29                return false;
30            }

31        }

 1/// <summary>
 2        /// MD5解密
 3        /// </summary>
 4        /// <param name="strValue"></param>
 5        /// <returns></returns>

 6        public static string MD5Decrypt(string strValue)
 7        {
 8            try
 9            {                
10                System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider();
11                byte[] result = md5.TransformFinalBlock(System.Text.Encoding.Default.GetBytes(strValue), 0, strValue.Length);
12                return System.Text.Encoding.Default.GetString(result);
13            }

14            catch(Exception ex)
15            {
16                Log.WriteDataBase(DateTime.Now,"3",FrmMain.userName,ex.Message,"主机名:"+System.Net.Dns.GetHostName()+";IP地址:"+FrmMain.addressList[0]);
17                return "";
18            }

19        }

 1/// <summary>
 2        /// MD5加密
 3        /// </summary>
 4        /// <param name="strValue"></param>
 5        /// <returns></returns>

 6        public static string MD5Encrypt(string strValue)
 7        {
 8            try
 9            {
10                MD5 md5= new MD5CryptoServiceProvider();
11                byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strValue));
12
13                return System.Text.Encoding.Default.GetString(result);
14            }

15            catch(Exception ex)
16            {
17                Log.WriteDataBase(DateTime.Now,"3",FrmMain.userName,ex.Message,"主机名:"+System.Net.Dns.GetHostName()+";IP地址:"+FrmMain.addressList[0]);
18                return "";
19            }

20        }
posted @ 2008-06-27 09:51  jinweida  阅读(1235)  评论(0编辑  收藏  举报