C# 操作注册表
时间:2011-05-09 来源:整天流浪的猫
using System; using Microsoft.Win32; namespace TunEc.ZHJF.Common { public class OperateReg { /// <summary> /// 读取指定名称的注册表的值 /// </summary> /// <param name="name">注册表值</param> /// <returns></returns> public static string GetRegData(string strItemName) { try { string registData; RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey("SOFTWARE", true); RegistryKey zhjf = software.CreateSubKey("ZHJF"); // RegistryKey zhjf = software.OpenSubKey("ZHJF", true); registData = zhjf.GetValue(strItemName.ToLower()).ToString(); return registData; } catch (Exception ex) { MessageboxX.ShowError(ex.Message); return ""; } } /// <summary> /// 注册表中写数据 /// </summary> /// <param name="name">注册表</param> /// <param name="tovalue">值</param> public static void WriteReg(string strItemName, string strItemValue) { try { RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey("SOFTWARE", true); RegistryKey zhjf = software.CreateSubKey("ZHJF"); zhjf.SetValue(strItemName.ToLower(), strItemValue); } catch (Exception ex) { MessageboxX.ShowError(ex.Message); } } /// <summary> /// .删除注册表中指定的注册表项 /// </summary> /// <param name="name">注册表</param> public static void DeleteRegItem(string strItemName) { try { RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey("SOFTWARE", true); RegistryKey zhjf = software.CreateSubKey("ZHJF"); zhjf.DeleteValue(strItemName.ToLower()); } catch (Exception ex) { MessageboxX.ShowError(ex.Message); } } /// <summary> /// 判断指定注册表项是否存在 /// </summary> /// <param name="name">注册表</param> /// <returns></returns> public static bool IsRegExit(string strItemName) { try { bool _exit = false; string[] subkeyNames; RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey("SOFTWARE", true); RegistryKey zhjf = software.CreateSubKey("ZHJF"); // RegistryKey zhjf = software.OpenSubKey("ZHJF", true); subkeyNames = zhjf.GetValueNames(); foreach (string keyName in subkeyNames) { if (keyName.ToLower() == strItemName.ToLower()) { _exit = true; return _exit; } } return _exit; } catch (Exception ex) { MessageboxX.ShowError(ex.Message); return false; } } } }
相关阅读 更多 +