在开发过程中,经常会遇到需要将有些信息写到注册表里,或者从注册表中读取信息。那该如何用.Net读写注册表呢?
.net 读写注册表:
/// <summary>
/// 获得注册表中的信息
/// </summary>
/// <returns>返回注册表中Item的值</returns>
public string GetRegisterKey()
{
string reVal = "";
RegistryKey softWareKey = Registry.CurrentUser.OpenSubKey("Software\\TestREG");
if (softWareKey != null)
{
object obj = softWareKey.GetValue(ItemName);
reVal = obj.ToString();
}
return reVal;
}
/// <summary>
/// 写注册表
/// </summary>
public int WriteRegisterKey(string ItemName, string Value)
{
int iReturn = 1;
try
{
RegistryKey regWrite = Registry.CurrentUser.CreateSubKey("SoftWare\\TestREG");
regWrite.SetValue(ItemName, Value);
regWrite.Close();
}
catch (Exception ex)
{
iReturn = 2;
throw ex;
}
return iReturn;
}