//测试注册表
//////////////////////////////////////////////////////////////////////////
/// <summary>
/// 读注册表
/// </summary>
/// <param name="path">获取路径</param>
/// <param name="name">要得到的值</param>
/// <returns>返回注册表指定键值</returns>
public static string GetRegisterData(string path,string name)
{
string registData = "";
RegistryKey hkml = Registry.CurrentUser;
RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
RegistryKey aimdir = software.OpenSubKey(path, true);
if (aimdir==null)
{
hkml.Close();
return registData;
}
registData = aimdir.GetValue(name).ToString();
hkml.Close();
return registData;
}
/// <summary>
/// 写注册表键值
/// </summary>
/// <param name="path">获取路径</param>
/// <param name="name">要设置的名字</param>
/// <param name="tovalue">要设置的值</param>
public static void WriteRegister(string path , string name, object tovalue)
{
RegistryKey hkml = Registry.CurrentUser;
RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
RegistryKey aimdir = software.CreateSubKey(path);
aimdir.SetValue(name, tovalue);
hkml.Close();
}
/// <summary>
/// 删除注册表值
/// </summary>
/// <param name="path">获取路径</param>
/// <param name="name">要删除的值</param>
public static void DeleteRegister(string path, string name)
{
string[] aimnames;
RegistryKey hkml = Registry.CurrentUser.OpenSubKey("SOFTWARE");
RegistryKey aimdir = hkml.OpenSubKey(path, true);
if (aimdir == null)
{
hkml.Close();
return;
}
aimnames = aimdir.GetSubKeyNames();
foreach (string aimKey in aimnames)
{
if (aimKey == name)
{
aimdir.DeleteSubKeyTree(name);
break;
}
}
hkml.Close();
}
/// <summary>
/// 判断注册表项是否存在
/// </summary>
/// <param name="path">获取路径</param>
/// <param name="name">要查询的值</param>
/// <returns>返回布尔值</returns>
public static bool IsRegisterItemExist(string path, string name)
{
bool _exit = false;
string[] subkeyNames;
RegistryKey hkml = Registry.CurrentUser.OpenSubKey("SOFTWARE");
RegistryKey aimdir = hkml.OpenSubKey(path, true);
if (aimdir == null)
{
hkml.Close();
return _exit;
}
subkeyNames = aimdir.GetSubKeyNames();
foreach (string keyName in subkeyNames)
{
if (keyName == name)
{
hkml.Close();
_exit = true;
return _exit;
}
}
hkml.Close();
return _exit;
}
/// <summary>
/// 判断值是否存在
/// </summary>
/// <param name="path">获取路径</param>
/// <param name="ValueName"></param>
/// <returns>返回布尔值</returns>
public static bool IsRegisterValueExit(string path, string ValueName)
{
string[] subkeyNames;
RegistryKey hkml = Registry.CurrentUser.OpenSubKey("SOFTWARE");
RegistryKey software = hkml.OpenSubKey(path, true);
if (software == null)
{
hkml.Close();
return false;
}
subkeyNames = software.GetValueNames();
foreach (string keyName in subkeyNames)
{
if (keyName == ValueName) //判断键值的名称
{
hkml.Close();
return true;
}
}
hkml.Close();
return false;
}
//应用示例
private void Form1_Load(object sender, EventArgs e)
{
//读取VC6
string sPath = @"Whole TomatoVisual Assist XVA6";
if (IsRegisterValueExit(sPath, "InstalledDir"))
{
textBox1.Text = GetRegisterData(sPath, "InstalledDir");
}
else
{
textBox1.Text = "不存在键值";
}
//读取VC2010
sPath = @"Whole TomatoVisual Assist XVANet10";
if (IsRegisterValueExit(sPath, "InstalledDir"))
{
textBox2.Text = GetRegisterData(sPath, "InstalledDir");
}
else
{
textBox1.Text = "不存在键值";
}
}