注册表操作类
能实现添加、删除、判断是否存在等操作
操作根目录为HKEY_LOCAL_MACHINE\SOFTWARE下。
using System;
using System.Collections.Generic;
using Microsoft.Win32;
namespace ImageExpressTest
{
/// <summary>
/// operate the Register
/// </summary>
public class RegisterOperate
{
/// <summary>
/// Creates the subkey.
/// location:HKEY_LOCAL_MACHINE\SOFTWARE
/// </summary>
/// <param name="strSubKey">The STR sub key.</param>
public static void CreateSubkey(string strSubKey)
{
try
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey softWare = hklm.OpenSubKey("SOFTWARE", true);
softWare.CreateSubKey(strSubKey);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Creates the subkey.
/// location:HKEY_LOCAL_MACHINE\SOFTWARE\parentkey
/// </summary>
/// <param name="parentkey">The parentkey.</param>
/// <param name="subkey">The subkey.</param>
public static void CreateSubkey(string strParentKey, string strSubKey)
{
try
{
//RegistryKey hklm = Registry.LocalMachine;
//RegistryKey softWare = hklm.OpenSubKey("SOFTWARE", true);
////RegistryKey parentKey = softWare.OpenSubKey(strParentKey);
RegistryKey parentKey = GetParentKey(strParentKey);
parentKey.CreateSubKey(strSubKey);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Creates the subkey and set default value
/// location:HKEY_LOCAL_MACHINE\SOFTWARE\parentkey
/// </summary>
/// <param name="strParentKey">The parent key.</param>
/// <param name="strSubKey">The sub key.</param>
/// <param name="strValue">The value.</param>
public static void CreateSubkey(string strParentKey, string strSubKey, string strValue)
{
try
{
//RegistryKey hklm = Registry.LocalMachine;
//RegistryKey softWare = hklm.OpenSubKey("SOFTWARE");
//RegistryKey parentKey = softWare.OpenSubKey(strParentKeyName);
RegistryKey parentKey = GetParentKey(strParentKey);
RegistryKey subkey = parentKey.CreateSubKey(strSubKey);
subkey.SetValue(null, strValue);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Sets the value.
/// location:HKEY_LOCAL_MACHINE\SOFTWARE\parentkey
/// </summary>
/// <param name="strParentKeyName">Name of the parent key.</param>
/// <param name="strName">Name.</param>
/// <param name="strValue">value.</param>
public static void SetValue(string strParentKey, string strName, string strValue)
{
try
{
//RegistryKey hklm = Registry.LocalMachine;
//RegistryKey softWare = hklm.OpenSubKey("SOFTWARE");
//RegistryKey parentKey = softWare.OpenSubKey(strParentKeyName);
RegistryKey parentKey = GetParentKey(strParentKey);
parentKey.SetValue(strName, strValue);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Deletes the key where location is HKEY_LOCAL_MACHINE\SOFTWARE\"
/// </summary>
/// <param name="strKeyName">Name of the key.</param>
public static void DeleteKey(string strKeyName)
{
try
{
List<string> aKeys = new List<string>();
RegistryKey hklm = Registry.LocalMachine;
RegistryKey softWare = hklm.OpenSubKey("SOFTWARE", true);
aKeys.AddRange(softWare.GetSubKeyNames());
if (aKeys.Contains(strKeyName))
{
softWare.DeleteSubKeyTree(strKeyName);
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Deletes the key and his subkeys where
/// location is HKEY_LOCAL_MACHINE\SOFTWARE\strParentKey".
/// </summary>
/// <param name="strParentKey">The parent key.</param>
/// <param name="strKeyName">Name of the key.</param>
public static void DeleteKey(string strParentKey, string strKeyName)
{
try
{
List<string> aKeys = new List<string>();
//RegistryKey hklm = Registry.LocalMachine;
//RegistryKey softWare = hklm.OpenSubKey("SOFTWARE");
//RegistryKey parentKey = softWare.OpenSubKey(strParentKeyName);
RegistryKey parentKey = GetParentKey(strParentKey);
aKeys.AddRange(parentKey.GetSubKeyNames());
if (aKeys.Contains(strKeyName))
{
parentKey.DeleteSubKeyTree(strKeyName);
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Determines whether the key named strKeyName is existed
/// </summary>
/// <param name="strKeyName">Name of the key.</param>
/// <returns>
/// <c>true</c> if key is existed; otherwise, <c>false</c>.
/// </returns>
public static bool IsKeyExisted(string strKeyName)
{
try
{
List<string> aKeys = new List<string>();
RegistryKey hklm = Registry.LocalMachine;
RegistryKey softWare = hklm.OpenSubKey("SOFTWARE");
aKeys.AddRange(softWare.GetSubKeyNames());
return aKeys.Contains(strKeyName);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Determines whether the key named strKeyName is existed
/// </summary>
/// <param name="strParentKeyName">Name of the parent key.</param>
/// <param name="strKeyName">Name of the key.</param>
/// <returns>
/// <c>true</c> if key is existed; otherwise, <c>false</c>.
/// </returns>
public static bool IsKeyExisted(string strParentKey, string strKeyName)
{
try
{
List<string> aKeys = new List<string>();
//RegistryKey hklm = Registry.LocalMachine;
//RegistryKey softWare = hklm.OpenSubKey("SOFTWARE");
//RegistryKey parentKey = softWare.OpenSubKey(strParentKeyName);
RegistryKey parentKey = GetParentKey(strParentKey);
aKeys.AddRange(parentKey.GetSubKeyNames());
return aKeys.Contains(strKeyName);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Gets the parent key.
/// </summary>
/// <param name="strParentKeyName">Name of the parent key.</param>
private static RegistryKey GetParentKey(string strParentKey)
{
try
{
List<string> aKeysTree = new List<string>();
char[] charSeparator = new char[]{'\\'};
aKeysTree.AddRange(strParentKey.Split(charSeparator,StringSplitOptions.RemoveEmptyEntries));
RegistryKey hklm = Registry.LocalMachine;
RegistryKey softWare = hklm.OpenSubKey("SOFTWARE");
RegistryKey parentKey = softWare;
foreach (string str in aKeysTree)
{
parentKey = parentKey.OpenSubKey(str, true);
}
return parentKey;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
using System.Collections.Generic;
using Microsoft.Win32;
namespace ImageExpressTest
{
/// <summary>
/// operate the Register
/// </summary>
public class RegisterOperate
{
/// <summary>
/// Creates the subkey.
/// location:HKEY_LOCAL_MACHINE\SOFTWARE
/// </summary>
/// <param name="strSubKey">The STR sub key.</param>
public static void CreateSubkey(string strSubKey)
{
try
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey softWare = hklm.OpenSubKey("SOFTWARE", true);
softWare.CreateSubKey(strSubKey);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Creates the subkey.
/// location:HKEY_LOCAL_MACHINE\SOFTWARE\parentkey
/// </summary>
/// <param name="parentkey">The parentkey.</param>
/// <param name="subkey">The subkey.</param>
public static void CreateSubkey(string strParentKey, string strSubKey)
{
try
{
//RegistryKey hklm = Registry.LocalMachine;
//RegistryKey softWare = hklm.OpenSubKey("SOFTWARE", true);
////RegistryKey parentKey = softWare.OpenSubKey(strParentKey);
RegistryKey parentKey = GetParentKey(strParentKey);
parentKey.CreateSubKey(strSubKey);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Creates the subkey and set default value
/// location:HKEY_LOCAL_MACHINE\SOFTWARE\parentkey
/// </summary>
/// <param name="strParentKey">The parent key.</param>
/// <param name="strSubKey">The sub key.</param>
/// <param name="strValue">The value.</param>
public static void CreateSubkey(string strParentKey, string strSubKey, string strValue)
{
try
{
//RegistryKey hklm = Registry.LocalMachine;
//RegistryKey softWare = hklm.OpenSubKey("SOFTWARE");
//RegistryKey parentKey = softWare.OpenSubKey(strParentKeyName);
RegistryKey parentKey = GetParentKey(strParentKey);
RegistryKey subkey = parentKey.CreateSubKey(strSubKey);
subkey.SetValue(null, strValue);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Sets the value.
/// location:HKEY_LOCAL_MACHINE\SOFTWARE\parentkey
/// </summary>
/// <param name="strParentKeyName">Name of the parent key.</param>
/// <param name="strName">Name.</param>
/// <param name="strValue">value.</param>
public static void SetValue(string strParentKey, string strName, string strValue)
{
try
{
//RegistryKey hklm = Registry.LocalMachine;
//RegistryKey softWare = hklm.OpenSubKey("SOFTWARE");
//RegistryKey parentKey = softWare.OpenSubKey(strParentKeyName);
RegistryKey parentKey = GetParentKey(strParentKey);
parentKey.SetValue(strName, strValue);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Deletes the key where location is HKEY_LOCAL_MACHINE\SOFTWARE\"
/// </summary>
/// <param name="strKeyName">Name of the key.</param>
public static void DeleteKey(string strKeyName)
{
try
{
List<string> aKeys = new List<string>();
RegistryKey hklm = Registry.LocalMachine;
RegistryKey softWare = hklm.OpenSubKey("SOFTWARE", true);
aKeys.AddRange(softWare.GetSubKeyNames());
if (aKeys.Contains(strKeyName))
{
softWare.DeleteSubKeyTree(strKeyName);
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Deletes the key and his subkeys where
/// location is HKEY_LOCAL_MACHINE\SOFTWARE\strParentKey".
/// </summary>
/// <param name="strParentKey">The parent key.</param>
/// <param name="strKeyName">Name of the key.</param>
public static void DeleteKey(string strParentKey, string strKeyName)
{
try
{
List<string> aKeys = new List<string>();
//RegistryKey hklm = Registry.LocalMachine;
//RegistryKey softWare = hklm.OpenSubKey("SOFTWARE");
//RegistryKey parentKey = softWare.OpenSubKey(strParentKeyName);
RegistryKey parentKey = GetParentKey(strParentKey);
aKeys.AddRange(parentKey.GetSubKeyNames());
if (aKeys.Contains(strKeyName))
{
parentKey.DeleteSubKeyTree(strKeyName);
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Determines whether the key named strKeyName is existed
/// </summary>
/// <param name="strKeyName">Name of the key.</param>
/// <returns>
/// <c>true</c> if key is existed; otherwise, <c>false</c>.
/// </returns>
public static bool IsKeyExisted(string strKeyName)
{
try
{
List<string> aKeys = new List<string>();
RegistryKey hklm = Registry.LocalMachine;
RegistryKey softWare = hklm.OpenSubKey("SOFTWARE");
aKeys.AddRange(softWare.GetSubKeyNames());
return aKeys.Contains(strKeyName);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Determines whether the key named strKeyName is existed
/// </summary>
/// <param name="strParentKeyName">Name of the parent key.</param>
/// <param name="strKeyName">Name of the key.</param>
/// <returns>
/// <c>true</c> if key is existed; otherwise, <c>false</c>.
/// </returns>
public static bool IsKeyExisted(string strParentKey, string strKeyName)
{
try
{
List<string> aKeys = new List<string>();
//RegistryKey hklm = Registry.LocalMachine;
//RegistryKey softWare = hklm.OpenSubKey("SOFTWARE");
//RegistryKey parentKey = softWare.OpenSubKey(strParentKeyName);
RegistryKey parentKey = GetParentKey(strParentKey);
aKeys.AddRange(parentKey.GetSubKeyNames());
return aKeys.Contains(strKeyName);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Gets the parent key.
/// </summary>
/// <param name="strParentKeyName">Name of the parent key.</param>
private static RegistryKey GetParentKey(string strParentKey)
{
try
{
List<string> aKeysTree = new List<string>();
char[] charSeparator = new char[]{'\\'};
aKeysTree.AddRange(strParentKey.Split(charSeparator,StringSplitOptions.RemoveEmptyEntries));
RegistryKey hklm = Registry.LocalMachine;
RegistryKey softWare = hklm.OpenSubKey("SOFTWARE");
RegistryKey parentKey = softWare;
foreach (string str in aKeysTree)
{
parentKey = parentKey.OpenSubKey(str, true);
}
return parentKey;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
测试类:
using System;
namespace ImageExpressTest
{
/// <summary>
/// Register ImageExpress component
/// </summary>
public static class RegisterImageExpress
{
#region const
const string COMPANY = "AVIT Solutions Apr. 11, 2002";
const string COPYRIGHT = "(c) 1998-2001 Pegasus Imaging Corp. All Rights Reserved.";
const string LICENSE = "Registered for use on a SINGLE computer to:";
const string SERIAL = "FKDKGKJDMBOLKHLPIPJDDNJBMFLDJIPPFEBFEALOEDMLEP";
const string USERNAME = "Your Company Name Here [reggie@avit.com.hk]";
#endregion
/// <summary>
/// Register ImageExpress component.
/// </summary>
public static void Register()
{
try
{
if (!RegisterOperate.IsKeyExisted("Pegasus Software"))
{
RegisterOperate.CreateSubkey("Pegasus Software");
RegisterOperate.CreateSubkey("Pegasus Software", "ImagXpress v6.00 ActiveX Control");
RegisterOperate.SetValue("Pegasus Software\\ImagXpress v6.00 ActiveX Control\\","DisplayName","D:\\PegasusSoftware\\ImagXpressv60");
RegisterOperate.CreateSubkey("Pegasus Software\\ImagXpress v6.00 ActiveX Control\\", "Company", COMPANY);
RegisterOperate.CreateSubkey("Pegasus Software\\ImagXpress v6.00 ActiveX Control\\", "Copyright", COPYRIGHT);
RegisterOperate.CreateSubkey("Pegasus Software\\ImagXpress v6.00 ActiveX Control\\", "License", LICENSE);
RegisterOperate.CreateSubkey("Pegasus Software\\ImagXpress v6.00 ActiveX Control\\", "Serial", SERIAL);
RegisterOperate.CreateSubkey("Pegasus Software\\ImagXpress v6.00 ActiveX Control\\", "UserName", USERNAME);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
namespace ImageExpressTest
{
/// <summary>
/// Register ImageExpress component
/// </summary>
public static class RegisterImageExpress
{
#region const
const string COMPANY = "AVIT Solutions Apr. 11, 2002";
const string COPYRIGHT = "(c) 1998-2001 Pegasus Imaging Corp. All Rights Reserved.";
const string LICENSE = "Registered for use on a SINGLE computer to:";
const string SERIAL = "FKDKGKJDMBOLKHLPIPJDDNJBMFLDJIPPFEBFEALOEDMLEP";
const string USERNAME = "Your Company Name Here [reggie@avit.com.hk]";
#endregion
/// <summary>
/// Register ImageExpress component.
/// </summary>
public static void Register()
{
try
{
if (!RegisterOperate.IsKeyExisted("Pegasus Software"))
{
RegisterOperate.CreateSubkey("Pegasus Software");
RegisterOperate.CreateSubkey("Pegasus Software", "ImagXpress v6.00 ActiveX Control");
RegisterOperate.SetValue("Pegasus Software\\ImagXpress v6.00 ActiveX Control\\","DisplayName","D:\\PegasusSoftware\\ImagXpressv60");
RegisterOperate.CreateSubkey("Pegasus Software\\ImagXpress v6.00 ActiveX Control\\", "Company", COMPANY);
RegisterOperate.CreateSubkey("Pegasus Software\\ImagXpress v6.00 ActiveX Control\\", "Copyright", COPYRIGHT);
RegisterOperate.CreateSubkey("Pegasus Software\\ImagXpress v6.00 ActiveX Control\\", "License", LICENSE);
RegisterOperate.CreateSubkey("Pegasus Software\\ImagXpress v6.00 ActiveX Control\\", "Serial", SERIAL);
RegisterOperate.CreateSubkey("Pegasus Software\\ImagXpress v6.00 ActiveX Control\\", "UserName", USERNAME);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}