一个注册表操作类,很有用!

using System;

using Microsoft.Win32;

 

namespace modify

{

     /// <summary>

     /// regedit 的摘要说明。

     /// </summary>

     public class regedit

     {

         public regedit()

         {

              //

              // TODO: 在此处添加构造函数逻辑

              //

         }

         /// <summary>

         /// 设置注册表值

         /// </summary>

         /// <param name="RegPath"></param>

         /// <param name="valuename"></param>

         /// <param name="keyvalue"></param>

         /// <returns></returns>

         public bool SetValue(string RegPath,string valuename,object keyvalue)

         {

              RegistryKey root=null;//基本的主键

              string sub_path="";//获取除了基本的主键字符窜的子字符窜

              if(RegPath.Length>=17&&RegPath.Substring(0,17)=="HKEY_CLASSES_ROOT")

              {

                   root=Microsoft.Win32.Registry.ClassesRoot;//

                   sub_path=RegPath.Substring(17).Substring(1);//获取子字符串,前面去掉“\

              }

              else if(RegPath.Length>=17&&RegPath.Substring(0,17)=="HKEY_CURRENT_USER")

              {

                   root=Microsoft.Win32.Registry.CurrentUser;

                   sub_path=RegPath.Substring(17).Substring(1);

              }

              else if(RegPath.Length>=18&&RegPath.Substring(0,18)=="HKEY_LOCAL_MACHINE")

              {

                   root=Microsoft.Win32.Registry.LocalMachine;

                   sub_path=RegPath.Substring(18).Substring(1);

              }

              else if(RegPath.Length>=10&&RegPath.Substring(0,10)=="HKEY_USERS")

              {

                   root=Microsoft.Win32.Registry.Users;

                   sub_path=RegPath.Substring(10).Substring(1);

              }

              else if(RegPath.Length>=19&&RegPath.Substring(0,19)=="HKEY_CURRENT_CONFIG")

              {

                   root=Microsoft.Win32.Registry.CurrentConfig;

                   sub_path=RegPath.Substring(19).Substring(1);

              }

              else

              {

                   return false;

              }

             

              RegistryKey fatherkey = root;

              string [] substringArray=sub_path.Split(new char[]{});

              for(int i=0;i<substringArray.Length;i++)

              {

                   RegistryKey thissubkey = fatherkey.OpenSubKey(substringArray[i],true);

                   if(thissubkey==null)

                   {

                       return false;

                   }

                   else

                   {

                       fatherkey = thissubkey;

                   }

              }

              fatherkey.SetValue(valuename,keyvalue);

              return true;

         }

         /// <summary>

         /// 得到注册表值

         /// </summary>

         /// <param name="RegPath"></param>

         /// <param name="valueName"></param>

         /// <returns></returns>

         public  object  GetValue(string RegPath,string valueName)

         {

              RegistryKey root=null;//基本的主键

              string sub_path="";//获取除了基本的主键字符窜的子字符窜

              object result= null;

              if(RegPath.Length>=17&&RegPath.Substring(0,17)=="HKEY_CLASSES_ROOT")

              {

                   root=Microsoft.Win32.Registry.ClassesRoot;//

                   sub_path=RegPath.Substring(17).Substring(1);//获取子字符串,前面去掉“\

              }

              else if(RegPath.Length>=17&&RegPath.Substring(0,17)=="HKEY_CURRENT_USER")

              {

                   root=Microsoft.Win32.Registry.CurrentUser;

                   sub_path=RegPath.Substring(17).Substring(1);

              }

              else if(RegPath.Length>=18&&RegPath.Substring(0,18)=="HKEY_LOCAL_MACHINE")

              {

                   root=Microsoft.Win32.Registry.LocalMachine;

                   sub_path=RegPath.Substring(18).Substring(1);

              }

              else if(RegPath.Length>=10&&RegPath.Substring(0,10)=="HKEY_USERS")

              {

                   root=Microsoft.Win32.Registry.Users;

                   sub_path=RegPath.Substring(10).Substring(1);

              }

              else if(RegPath.Length>=19&&RegPath.Substring(0,19)=="HKEY_CURRENT_CONFIG")

              {

                   root=Microsoft.Win32.Registry.CurrentConfig;

                   sub_path=RegPath.Substring(19).Substring(1);

              }

              else

              {

                   root=null;

                   return("no root");

              }

              RegistryKey fatherkey = root;

              string [] substringArray=sub_path.Split(new char[]{});

              for(int i=0;i<substringArray.Length;i++)

              {

                   RegistryKey thissubkey = fatherkey.OpenSubKey(substringArray[i]);

                   if(thissubkey==null)

                   {

                       return ("["+substringArray[i] +"] is not found");

                   }

                   else

                   {

                       fatherkey = thissubkey;

                   }

              }

              result=fatherkey.GetValue(valueName);

              if(result==null)

              {

                   return ("no value");

              }

              else

              {

                   return result;

              }

         }

     }

}

 

posted on 2004-06-10 09:21  edobnet  阅读(1465)  评论(1编辑  收藏  举报