Operating the registry help class(C#读写注册表操作)
2012-08-08 09:38
621 查看
/// <summary>
/// Operating the registry help class
/// </summary>
public class RegistryHelper
{
/// <summary>
/// Gets value by access registry in 'CurrentUser' path
/// </summary>
/// <param name="regPath">registry path</param>
/// <param name="key">registry key</param>
/// <returns>registry value</returns>
public static string GetRegByCurrentUserValue(string regPath, string key)
{
string res = string.Empty;
try
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(regPath, RegistryKeyPermissionCheck.Default, System.Security.AccessControl.RegistryRights.ReadKey);
if (registryKey != null)
{
var value = registryKey.GetValue(key);
res = value == null ? string.Empty : value.ToString();
registryKey.Close();
}
}
catch
{
return string.Empty;
}
return res;
}
/// <summary>
/// Gets value by access registry in 'LocalMachine' path
/// </summary>
/// <param name="regPath">registry path</param>
/// <param name="key">registry key</param>
/// <returns>registry value</returns>
public static string GetRegByLocalMachineValue(string regPath, string key)
{
string res = string.Empty;
try
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(regPath,RegistryKeyPermissionCheck.Default,System.Security.AccessControl.RegistryRights.ReadKey);
if (registryKey != null)
{
var value = registryKey.GetValue(key);
res = value == null ? string.Empty : value.ToString();
registryKey.Close();
}
}
catch (Exception ex)
{
LogHelper.WriteException(ex);
return string.Empty;
}
return res;
}
/// <summary>
/// insert or update registry value in 'CurrentUser' path
/// </summary>
/// <param name="regPath">registry path</param>
/// <param name="key">registry key</param>
/// <param name="setValue">registry value</param>
/// <returns>bool</returns>
public static bool ToInsertOrUpdateKey(string regPath, string key, string setValue)
{
bool res = false;
try
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(regPath, true);
if (registryKey == null)
{
//Create KEY
registryKey = Registry.CurrentUser.CreateSubKey(regPath);
}
if (registryKey != null)
{
//update Key,value
registryKey.SetValue(key, setValue);//not exists add
res = true;
registryKey.Close();
}
}
catch
{
return false;
}
return res;
}
}
/// Operating the registry help class
/// </summary>
public class RegistryHelper
{
/// <summary>
/// Gets value by access registry in 'CurrentUser' path
/// </summary>
/// <param name="regPath">registry path</param>
/// <param name="key">registry key</param>
/// <returns>registry value</returns>
public static string GetRegByCurrentUserValue(string regPath, string key)
{
string res = string.Empty;
try
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(regPath, RegistryKeyPermissionCheck.Default, System.Security.AccessControl.RegistryRights.ReadKey);
if (registryKey != null)
{
var value = registryKey.GetValue(key);
res = value == null ? string.Empty : value.ToString();
registryKey.Close();
}
}
catch
{
return string.Empty;
}
return res;
}
/// <summary>
/// Gets value by access registry in 'LocalMachine' path
/// </summary>
/// <param name="regPath">registry path</param>
/// <param name="key">registry key</param>
/// <returns>registry value</returns>
public static string GetRegByLocalMachineValue(string regPath, string key)
{
string res = string.Empty;
try
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(regPath,RegistryKeyPermissionCheck.Default,System.Security.AccessControl.RegistryRights.ReadKey);
if (registryKey != null)
{
var value = registryKey.GetValue(key);
res = value == null ? string.Empty : value.ToString();
registryKey.Close();
}
}
catch (Exception ex)
{
LogHelper.WriteException(ex);
return string.Empty;
}
return res;
}
/// <summary>
/// insert or update registry value in 'CurrentUser' path
/// </summary>
/// <param name="regPath">registry path</param>
/// <param name="key">registry key</param>
/// <param name="setValue">registry value</param>
/// <returns>bool</returns>
public static bool ToInsertOrUpdateKey(string regPath, string key, string setValue)
{
bool res = false;
try
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(regPath, true);
if (registryKey == null)
{
//Create KEY
registryKey = Registry.CurrentUser.CreateSubKey(regPath);
}
if (registryKey != null)
{
//update Key,value
registryKey.SetValue(key, setValue);//not exists add
res = true;
registryKey.Close();
}
}
catch
{
return false;
}
return res;
}
}
相关文章推荐
- C# 操作注册表WindowsRegistry
- C#中关于文件读写操作和注册表操作
- C#操作注册表(读写)
- c# RegistryKey操作注册表
- C#操作注册表REG_DWORD类型键异常(RegistryValueKind.DWord异常)解决方案
- C#操作注册表REG_DWORD类型键异常(RegistryValueKind.DWord异常)解决方案
- c# RegistryKey注册表操作
- C#操作注册表(读写)
- c#.net操作注册表RegistryKey
- C#中对注册表的操作 读写注册表方法
- C# 操作注册表
- C#对注册表的操作
- c#中XML文件的读写操作
- 一个开源的C#工具类库,有校验、加密、注册表操作等功能,share给大家
- c# 注册表操作,创建,删除,修改,判断节点是否存在
- C# 读写注册表
- C#操作注册表
- 基于C#操作注册表
- [我眼中的C#]文件读写操作
- C#操作Xml:通过XmlDocument读写Xml文档