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());
}