您的位置:首页 > 其它

创建注册表读写程序(二)

2014-11-06 17:39 246 查看
往注册表中写入数据是大部分常见的Windows系统优化工具的根本方法,因此只要掌握了写入的方法且对注册表有足够的了解,读者完全可以用C#实现一个自己的系统优化工具。

实例如下:

using Mic
a8ae
rosoft.Win32;

class program

{

   static void Main(string[ ] args)

   {

      SetRegistData("My Value");//写入数据

      Console.WriteLine("写入注册表成功!");

      Console.WriteLine("读取到的值为:");

      Console.WriteLine(GetRegistData());//读取数据

      SetRegistData(@ "C:\WINDOWS\system32");//恢复原始数据

   }

    static string GetRegistData()

    {

       string registData;//用于保存读取值的变量

      //用hkml表示注册表的根键"HKEY_LOCAL_MACHINE"

      RegistryKey  hkml=Registry.LocalMachine;

      //获得根键"HKEY_LOCAL_MACHINE"下的"Software"子键

      RegistryKey  software=hkml.OpenSubKey("SOFTWARE",true);

      //获得子键"HKEY_LOCAL_MACHINE"下的"Microsoft"子键

      RegistryKey  microsoft=software.OpenSubKey("Microsoft",true);

      //获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft"下的"Windows"子键

     RegistryKey  windows=microsoft.OpenSubKey("Windows",true);

     //获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows"下的"Help"子键

     RegistryKey  help=windows.OpenSubKey("Help",true);

     //获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\Help"下的"PINTLPAE.HLP"的值

     registDatahelp.GetValue(@ "PINTLPAE.HLP").ToString();

     Console.WriteLine("读取注册表成功");

     return  registData;

    }

    static void SetRegistData(string myValue)

    {

       //用hkml表示注册表的根键"HKEY_LOCAL_MACHINE"       

       RegistryKey  hkml=Registry.LocalMachine;

      //获得根键"HKEY_LOCAL_MACHINE"下的"Software"子键

      RegistryKey  software=hkml.OpenSubKey("SOFTWARE",true);

      //获得子键"HKEY_LOCAL_MACHINE"下的"Microsoft"子键

      RegistryKey  microsoft=software.OpenSubKey("Microsoft",true);

      //获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft"下的"Windows"子键

     RegistryKey  windows=microsoft.OpenSubKey("Windows",true);

     //获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows"下的"Help"子键

     RegistryKey  help=windows.OpenSubKey("Help",true);

     //获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\Help"下的"PINTLPAE.HLP"的值

     registDatahelp.SetValue(@ "PINTLPAE.HLP",myValue).ToString();

    }

    //删除注册表的值

    static void DelRegistData(string myValue)

    {

       

//用hkml表示注册表的根键"HKEY_LOCAL_MACHINE"       

       RegistryKey  hkml=Registry.LocalMachine;

      //获得根键"HKEY_LOCAL_MACHINE"下的"Software"子键

      RegistryKey  software=hkml.OpenSubKey("SOFTWARE",true);

      //获得子键"HKEY_LOCAL_MACHINE"下的"Microsoft"子键

      RegistryKey  microsoft=software.OpenSubKey("Microsoft",true);

      //获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft"下的"Windows"子键

     RegistryKey  windows=microsoft.OpenSubKey("Windows",true);

     //获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows"下的"Help"子键

     RegistryKey  help=windows.OpenSubKey("Help",true);

      //删除子键“HKEY_LOCAL_MACHINE\Software\Microsoft\Windows|Help"下的"myValue"值

      help.DeleteValue(myValue,false);

    }

}

获取注册表中的所有值项

class program

{

    static void Main(string[ ] args)

    {

       //读取所有该子键下的值项

       string[ ] names=ListRegistData("myValue");

       //列出所有值项

       foreach(string name in names)

       {

          Console.WriteLine(name);

       }

    }

      static string[ ] ListRegistData(string myValue)

      {

        RegistryKey  help=EnterValue();

        string[ ] names=help.GetValueNames();

        return names;

      }

      private static RegistryKey EnterValue()

      {

         //用hkml表示注册表的根键"HKEY_LOCAL_MACHINE"

         

RegistryKey  hkml=Registry.LocalMachine;

      //获得根键"HKEY_LOCAL_MACHINE"下的"Software"子键

      RegistryKey  software=hkml.OpenSubKey("SOFTWARE",true);

      //获得子键"HKEY_LOCAL_MACHINE"下的"Microsoft"子键

      RegistryKey  microsoft=software.OpenSubKey("Microsoft",true);

      //获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft"下的"Windows"子键

     RegistryKey  windows=microsoft.OpenSubKey("Windows",true);

     //获得子键"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows"下的"Help"子键

     RegistryKey  help=windows.OpenSubKey("Help",true);

      return help;

      }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: