public virtual bool WriteRegeditKey(string name, object content, RegValueKind regValueKind) { bool result = false; if (name == string.Empty || name == null) { return(false); } if (!this.IsSubKeyExist(this._subkey)) { this.CreateSubKey(this._subkey); } RegistryKey registryKey = this.OpenSubKey(true); if (registryKey == null) { return(false); } try { registryKey.SetValue(name, content, this.GetRegValueKind(regValueKind)); result = true; } catch { result = false; } finally { registryKey.Close(); } return(result); }
/// <summary> /// 获取在注册表中对应的值数据类型 /// 例子:如regValueKind是DWord,则返回RegistryValueKind.DWord /// </summary> /// <param name="regValueKind">注册表数据类型</param> /// <returns>注册表中对应的数据类型</returns> protected RegistryValueKind GetRegValueKind(RegValueKind regValueKind) { RegistryValueKind regValueK; #region 判断注册表数据类型 switch (regValueKind) { case RegValueKind.Unknown: regValueK = RegistryValueKind.Unknown; break; case RegValueKind.String: regValueK = RegistryValueKind.String; break; case RegValueKind.ExpandString: regValueK = RegistryValueKind.ExpandString; break; case RegValueKind.Binary: regValueK = RegistryValueKind.Binary; break; case RegValueKind.DWord: regValueK = RegistryValueKind.DWord; break; case RegValueKind.MultiString: regValueK = RegistryValueKind.MultiString; break; case RegValueKind.QWord: regValueK = RegistryValueKind.QWord; break; default: regValueK = RegistryValueKind.String; break; } #endregion return(regValueK); }
public virtual bool SetInfo(string name, object content, RegValueKind regValueKind) { if (name == string.Empty || name == null) { return(false); } RegistryKey key = InnerKey; bool result = false; try { key.SetValue(name, content, GetRegValueKind(regValueKind)); result = true; } catch (Exception ex) { Console.WriteLine(string.Format("Reg.writeReg({0},{1},{2}).{3}", name, content, regValueKind.ToString(), ex.Message)); result = false; } finally { key.Close(); } return(result); }
/// <summary> /// 设置指定的键值内容,不指定内容数据类型(请先设置SubKey属性) /// 存在改键值则修改键值内容,不存在键值则先创建键值,再设置键值内容 /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="regValueKind"></param> /// <returns></returns> public virtual bool WriteRegKey(string key, object value, RegValueKind regValueKind) { //返回结果 bool result = false; //判断键值是否存在 if (key == string.Empty || key == null) { return(false); } //判断注册表项是否存在,如果不存在,则直接创建 if (!IsSubKeyExist(REG_CUR_ROOT)) { CreateSubKey(REG_CUR_ROOT); } //以可写方式打开注册表项 using (RegistryKey item = OpenSubPathWithMS(REG_CUR_ROOT)) { //如果注册表项打开失败,则返回false if (key == null) { return(false); } item.SetValue(key, value, GetRegValueKind(regValueKind)); result = true; //关闭对注册表项的更改 item.Close(); } return(result); }
/// <summary> /// 设置指定的键值内容,指定内容数据类型(请先设置SubKey属性) /// 存在改键值则修改键值内容,不存在键值则先创建键值,再设置键值内容 /// </summary> /// <param name="name">键值名称</param> /// <param name="content">键值内容</param> /// <returns>键值内容设置成功,则返回true,否则返回false</returns> public virtual bool WriteRegeditKey(string name, object content, RegValueKind regValueKind) { ///返回结果 bool result = false; ///判断键值是否存在 if (name == string.Empty || name == null) { return(false); } ///判断注册表项是否存在,如果不存在,则直接创建 if (!IsSubKeyExist(_subkey)) { CreateSubKey(_subkey); } ///以可写方式打开注册表项 RegistryKey key = OpenSubKey(true); ///如果注册表项打开失败,则返回false if (key == null) { return(false); } try { key.SetValue(name, content, GetRegValueKind(regValueKind)); result = true; } catch { result = false; } finally { ///关闭对注册表项的更改 key.Close(); } return(result); }
protected RegistryValueKind GetRegValueKind(RegValueKind regValueKind) { RegistryValueKind result; switch (regValueKind) { case RegValueKind.Unknown: result = RegistryValueKind.Unknown; break; case RegValueKind.String: result = RegistryValueKind.String; break; case RegValueKind.ExpandString: result = RegistryValueKind.ExpandString; break; case RegValueKind.Binary: result = RegistryValueKind.Binary; break; case RegValueKind.DWord: result = RegistryValueKind.DWord; break; case RegValueKind.MultiString: result = RegistryValueKind.MultiString; break; case RegValueKind.QWord: result = RegistryValueKind.QWord; break; default: result = RegistryValueKind.String; break; } return(result); }
public virtual bool SetInfo(string name, object content, RegValueKind regValueKind) { if (string.IsNullOrEmpty(name)) { return(false); } RegistryKey key = InnerKey; bool result = false; try { key.SetValue(name, content, GetRegValueKind(regValueKind)); result = true; } catch (Exception) { result = false; } finally { key.Close(); } return(result); }
/// <summary> /// 获取在注册表中对应的值数据类型 /// 例子:如regValueKind是DWord,则返回RegistryValueKind.DWord /// </summary> /// <param name="regValueKind">注册表数据类型</param> /// <returns>注册表中对应的数据类型</returns> protected RegistryValueKind GetRegValueKind(RegValueKind regValueKind) { RegistryValueKind regValueK; #region 判断注册表数据类型 switch (regValueKind) { case RegValueKind.Unknown: regValueK = RegistryValueKind.Unknown; break; case RegValueKind.String: regValueK = RegistryValueKind.String; break; case RegValueKind.ExpandString: regValueK = RegistryValueKind.ExpandString; break; case RegValueKind.Binary: regValueK = RegistryValueKind.Binary; break; case RegValueKind.DWord: regValueK = RegistryValueKind.DWord; break; case RegValueKind.MultiString: regValueK = RegistryValueKind.MultiString; break; case RegValueKind.QWord: regValueK = RegistryValueKind.QWord; break; default: regValueK = RegistryValueKind.String; break; } #endregion return regValueK; }
/// <summary> /// 设置指定的键值内容,指定内容数据类型(请先设置SubKey属性) /// 存在改键值则修改键值内容,不存在键值则先创建键值,再设置键值内容 /// </summary> /// <param name="name">键值名称</param> /// <param name="content">键值内容</param> /// <returns>键值内容设置成功,则返回true,否则返回false</returns> public virtual bool WriteRegeditKey(string name, object content, RegValueKind regValueKind) { ///返回结果 bool result = false; ///判断键值是否存在 if (name == string.Empty || name == null) { return false; } ///判断注册表项是否存在,如果不存在,则直接创建 if (!IsSubKeyExist(_subkey)) { CreateSubKey(_subkey); } ///以可写方式打开注册表项 RegistryKey key = OpenSubKey(true); ///如果注册表项打开失败,则返回false if (key == null) { return false; } try { key.SetValue(name, content, GetRegValueKind(regValueKind)); result = true; } catch { result = false; } finally { ///关闭对注册表项的更改 key.Close(); } return result; }
public static extern uint RegQueryValueEx(IntPtr hKey, string lpValueName, int lpReserved, ref RegValueKind lpType, IntPtr lpData, ref int lpcbData);
/// <summary> /// 获取在注册表中对应的值数据类型 /// </summary> /// <param name="regValueKind">注册表数据类型</param> /// <returns>注册表中对应的数据类型</returns> static RegistryValueKind GetRegValueKind(RegValueKind regValueKind) { switch (regValueKind) { case RegValueKind.Unknown: return RegistryValueKind.Unknown; case RegValueKind.String: return RegistryValueKind.String; case RegValueKind.ExpandString: return RegistryValueKind.ExpandString; case RegValueKind.Binary: return RegistryValueKind.Binary; case RegValueKind.DWord: return RegistryValueKind.DWord; case RegValueKind.MultiString: return RegistryValueKind.MultiString; case RegValueKind.QWord: return RegistryValueKind.QWord; default: return RegistryValueKind.String; } }
/// <summary> /// 使用指定的注册表数据类型设置注册表项中的名称/值对的值。 /// </summary> /// <param name="name">要存储的值的名称。</param> /// <param name="value">要存储的数据。</param> /// <param name="subkey">子项的名称或路径。</param> /// <param name="regDomain">注册表基项</param> /// <param name="regValueKind">在存储数据时使用的注册表数据类型。</param> public static void SetValue(string name, object value, string subkey, RegDomain regDomain, RegValueKind regValueKind) { var sub = GetSubKey(subkey, true, regDomain); if (sub != null) { sub.SetValue(name, value, GetRegValueKind(regValueKind)); sub.Close(); } }
/// <summary> /// 设置指定的键值内容,指定内容数据类型(请先设置 SubKey 属性) /// 存在改键值则修改键值内容,不存在键值则先创建键值,再设置键值内容 /// </summary> /// <param name="name">键值名称</param> /// <param name="content">键值内容</param> /// <returns>键值内容设置成功,则返回 true,否则返回 false</returns> public virtual bool WriteRegeditKey(string name, object content, RegValueKind regValueKind) { return(WriteRegeditKey(name, _subkey, _domain, content, regValueKind)); }
public virtual bool WriteRegeditKey(string name, string subkey, RegDomain domain, object content, RegValueKind regValueKind) { //返回结果 bool result = false; //判断注册表项是否存在,如果不存在,则直接创建 if (!IsSubKeyExist(subkey, domain)) { CreateSubKey(subkey, domain); } if (string.IsNullOrEmpty(name)) { return(true); } //以可写方式打开注册表项 RegistryKey key = OpenSubKey(subkey, domain, true); //如果注册表项打开失败,则返回 false if (key == null) { return(false); } try { key.SetValue(name, content, GetRegValueKind(regValueKind)); result = true; } catch (Exception e) { Log.Debug(e); result = false; } finally { //关闭对注册表项的更改 key.Close(); } return(result); }