C#中注册表的写入和读取

//写入注册表

   try
   {
    Microsoft.Win32.RegistryKey res ;
    res = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\testname",true);
     if (res == null)
    {
     try
     {
      res = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"SOFTWARE\testname");
     }
     catch (Exception e)
     {
      MessageBox.Show(e.Message.ToString());
     }
    
    }
  
    res.SetValue("test",this.txtWrite.Text );
    MessageBox.Show("成功写入注册表!");
   }
   catch(Exception e)
   {
   MessageBox.Show(e.Message.ToString());
   }

//读取注册表

try
   {
    Microsoft.Win32.RegistryKey res;
    res = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\testname",true);
    this.txtRead.Text= res.GetValue("test").ToString() ;
   }
   catch(Exception ee)
   {
    MessageBox.Show(e.Message.ToString());
   }

posted @ 2008-10-23 10:52  Landy_di  阅读(267)  评论(0编辑  收藏  举报