using Microsoft.Win32;
/// <summary>
/// 得到電腦名
/// </summary>
string sComputerName = System.Environment.MachineName;
/// 得到電腦名
/// </summary>
string sComputerName = System.Environment.MachineName;
//注冊表中的路徑「SOFTWARE\CmWeb」
const string sRegistryPath = @"SOFTWARE\CmWeb";
const string sRegistryPath = @"SOFTWARE\CmWeb";
1、讀取注冊表
/// <summary>
/// 得到注冊表內容
/// </summary>
void GetRegistry()
{
//打開指定路麼的注冊表
rkKey = Registry.LocalMachine.OpenSubKey(sRegistryPath);
if (rkKey != null)
{
//得到注冊表內容
string sRegistryValue = rkKey.GetValue(sRegistryKey, "").ToString();
}
}
/// 得到注冊表內容
/// </summary>
void GetRegistry()
{
//打開指定路麼的注冊表
rkKey = Registry.LocalMachine.OpenSubKey(sRegistryPath);
if (rkKey != null)
{
//得到注冊表內容
string sRegistryValue = rkKey.GetValue(sRegistryKey, "").ToString();
}
}
2、創建注冊表
/// <summary>
/// 創建注冊表內容
/// </summary>
private void CreateRegistry()
{
//得到「HKEY_LOCAL_MACHINE\SOFTWARE」主鍵
RegistryKey rkRootKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE",true);
if (rkRootKey != null)
{
//不存在CmWeb時創建CmWeb
if (Array.IndexOf(rkRootKey.GetSubKeyNames(), "CmWeb") < 0)
{
rkRootKey.CreateSubKey("CmWeb");
}
//得到「HKEY_LOCAL_MACHINE\SOFTWARE\CMWEB」鍵
rkKey = Registry.LocalMachine.OpenSubKey(sRegistryPath,true);
if (rkKey != null)
{
//電腦名
string sRegValue = sComputerName;
//創建鍵值
rkKey.SetValue(sRegistryKey, sRegValue);
}
}
}
/// 創建注冊表內容
/// </summary>
private void CreateRegistry()
{
//得到「HKEY_LOCAL_MACHINE\SOFTWARE」主鍵
RegistryKey rkRootKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE",true);
if (rkRootKey != null)
{
//不存在CmWeb時創建CmWeb
if (Array.IndexOf(rkRootKey.GetSubKeyNames(), "CmWeb") < 0)
{
rkRootKey.CreateSubKey("CmWeb");
}
//得到「HKEY_LOCAL_MACHINE\SOFTWARE\CMWEB」鍵
rkKey = Registry.LocalMachine.OpenSubKey(sRegistryPath,true);
if (rkKey != null)
{
//電腦名
string sRegValue = sComputerName;
//創建鍵值
rkKey.SetValue(sRegistryKey, sRegValue);
}
}
}
3、更新注冊表
/// <summary>
/// 更新注冊表
/// </summary>
void UpdateRegistry()
{
//得到「HKEY_LOCAL_MACHINE\SOFTWARE」主鍵
RegistryKey rkRootKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE",true);
if (rkRootKey != null)
{
//不存在「CmWeb」鍵時創建CmWeb
if (Array.IndexOf(rkRootKey.GetSubKeyNames(), "CmWeb") < 0)
{
//創建鍵名
rkRootKey.CreateSubKey("CmWeb");
}
//得到「HKEY_LOCAL_MACHINE\SOFTWARE\CMWEB」鍵
rkKey = Registry.LocalMachine.OpenSubKey(sRegistryPath,true);
if (rkKey != null)
{
//電腦名
string sRegValue = sComputerName;
//創建鍵值
rkKey.SetValue(sRegistryKey, sRegValue);
}
}
}