C#操作注册表
#region 操作注册表 RegistryKey key = Registry.CurrentUser; string regPath = "software\\MyApp"; /// <summary> /// 创建注册表 /// 注意:注册表路径一定是"\\"而不是"\" /// </summary> public void CreateRrg() { RegistryKey software = key.CreateSubKey(regPath); software.Close(); } /// <summary> /// 写入注册表 /// </summary> /// <param name="regPath">"software\\MyApp"</param> /// <param name="field">"ProductID"</param> /// <param name="value">"Leo Tian's blog"</param> public void WriteToReg(string field, string value) { RegistryKey software = key.OpenSubKey(regPath, true); software.SetValue(field, value); software.Close(); } /// <summary> /// 读取 /// </summary> public string ReadRegistry(string str) { string info = string.Empty; RegistryKey software = key.OpenSubKey(regPath, true); if (software != null) { info = software.GetValue(str).ToString(); software.Close(); } return info; } /// <summary> /// 删除注册表 /// </summary> public void DeleteReg() { //删除 key.DeleteSubKey(regPath, true); } public bool IsRegeditItemExist() { string[] subkeyNames; RegistryKey software = key.OpenSubKey("software", true); subkeyNames = software.GetSubKeyNames(); //取得该项下所有子项的名称的序列,并传递给预定的数组中 foreach (string keyName in subkeyNames) { if (keyName == "MyApp") { var value = Registry.GetValue("HKEY_CURRENT_USER\\Software\\MyApp", "RegisterCode", null); var valueTime = Registry.GetValue("HKEY_CURRENT_USER\\Software\\MyApp", "RegisterTime", null); if (value != null && valueTime!=null ) { return true; } else { return false; } } } return false; } #endregion