public static Boolean Exists(RegistryKeyType type, string path) { Boolean bExist = false; RegistryKey rk; if (type == RegistryKeyType.CurrentUser) { rk = Registry.CurrentUser; } else { rk = Registry.LocalMachine; } RegistryKey registry = rk.OpenSubKey(path); if (registry == null) { return(bExist); } bExist = true; registry.Close(); return(bExist); }
private void SetRegistryKey(String KeyName, String SubKeyName, String Value, RegistryKeyType myType) { String ActualValue = ""; if (myType == RegistryKeyType.String) { RegistryValueKind myRegistryValueKind = RegistryValueKind.String; try { Registry.SetValue(KeyName, SubKeyName, Value, myRegistryValueKind); ActualValue = (String)Registry.GetValue(KeyName, SubKeyName, ""); } catch (Exception e) { // do nothing. } } if (myType == RegistryKeyType.DWord) { RegistryValueKind myRegistryValueKind = RegistryValueKind.DWord; try { int ValueCopy_Int = Convert.ToInt32(Value); Registry.SetValue(KeyName, SubKeyName, ValueCopy_Int, myRegistryValueKind); ActualValue = Convert.ToString(Registry.GetValue(KeyName, SubKeyName, "")); } catch (Exception e) { // do nothing. } } richTextBox_Status.Text += "Setting " + SubKeyName + " to: " + ActualValue + "\n"; }
private void lvKeyDisplay_DoubleClick(object sender, EventArgs e) { TreeNode selectedNode = tvRegistryNavigator.SelectedNode; if (selectedNode == null) { return; } if (lvKeyDisplay.SelectedItems.Count < 1) { return; } if (lvKeyDisplay.View == View.List) { return; } string key = lvKeyDisplay.SelectedItems[0].Text; using (editRegistryKeyForm erkf = new editRegistryKeyForm(key)) { if (erkf.ShowDialog() == DialogResult.OK) { RegistryKeyType regKey = keyFromNode(GetRootNode(selectedNode)); RegistryNodeData data = (RegistryNodeData)selectedNode.Tag; Client.Send((byte)NetworkCommand.RegistryEdit, (byte)RegistryCommand.SetValue, (byte)regKey, data.Path, key, erkf.NewValue); data.LoadedValues = false; LoadValues(selectedNode); } } }
static extern int NtSetValueKey( SafeRegistryHandle KeyHandle, UnicodeString ValueName, int TitleIndex, RegistryKeyType Type, byte[] Data, int DataSize);
private void deleteToolStripMenuItem_Click(object sender, EventArgs e) { TreeNode selectedNode = tvRegistryNavigator.SelectedNode; if (selectedNode == null) { return; } if (lvKeyDisplay.SelectedItems.Count < 1) { return; } if (lvKeyDisplay.View == View.List) { return; } string key = lvKeyDisplay.SelectedItems[0].Text; if (MessageBox.Show(string.Format("Delete the registry key {0}?", key), "Delete registry key", MessageBoxButtons.YesNo) == DialogResult.Yes) { RegistryKeyType regKey = keyFromNode(GetRootNode(selectedNode)); RegistryNodeData data = (RegistryNodeData)selectedNode.Tag; Client.Send((byte)NetworkCommand.RegistryEdit, (byte)RegistryCommand.DeleteValue, (byte)regKey, data.Path, key); data.LoadedValues = false; LoadValues(selectedNode); } }
void LoadValues(TreeNode Node) { if (Node == null) { return; } if (IsLoading) { return; } RegistryNodeData data = (RegistryNodeData)Node.Tag; if (data.LoadedValues) { UpdateVauesFromRegistryData(data); return; } IsLoading = true; UpdatingNode = Node; TreeNode rootNode = GetRootNode(Node); Invoke((MethodInvoker) delegate() { lvKeyDisplay.Items.Clear(); lvKeyDisplay.View = View.List; lvKeyDisplay.Items.Add("Loading..."); }); RegistryKeyType key = keyFromNode(rootNode); Client.Send((byte)NetworkCommand.RegistryEdit, (byte)RegistryCommand.UpdateKeys, (byte)key, data.Path); }
/// <summary> /// Writes the value. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="type">The type.</param> /// <param name="regKey">The reg key.</param> /// <param name="name">The name.</param> /// <param name="value">The value.</param> /// <param name="valueKind">Kind of the value.</param> public static void WriteValue <T>(RegistryKeyType type, string regKey, string name, T value, RegistryValueKind valueKind) { RegistryKey oRegKey = null; switch (type) { case RegistryKeyType.CurrentUser: oRegKey = Registry.CurrentUser; break; case RegistryKeyType.LocalMachine: oRegKey = Registry.LocalMachine; break; } try { oRegKey = oRegKey.OpenSubKey(regKey, true); oRegKey.GetValue(name); Debug.WriteLine("Leggo " + name); oRegKey.SetValue(name, value, valueKind); } finally { oRegKey.Close(); } }
/// <summary> /// 检索指定项关联的所有值 /// </summary> /// <param name="keytype">基项枚举</param> /// <param name="key">指定项字符串</param> /// <returns>返回指定项关联的所有值的字符串数组</returns> public string[] GetValues(RegistryKeyType keytype, string key) { RegistryKey rk = (RegistryKey)GetRegistryKey(keytype); RegistryKey software = rk.OpenSubKey(baseKey, true); RegistryKey rkt = software.OpenSubKey(key); string[] names = rkt.GetValueNames(); if (names.Length == 0) { return(names); } else { string[] values = new string[names.Length]; int i = 0; foreach (string name in names) { values[i] = rkt.GetValue(name).ToString(); i++; } return(values); } }
/// <summary> /// 返回RegistryKey对象 /// </summary> /// <param name="keyType">注册表基项枚举</param> /// <returns></returns> private object GetRegistryKey(RegistryKeyType keyType) { if (keyType.Equals(RegistryKeyType.HKEY_CLASS_ROOT)) { return(Registry.ClassesRoot); } if (keyType.Equals(RegistryKeyType.HKEY_CURRENT_USER)) { return(Registry.CurrentUser); } if (keyType.Equals(RegistryKeyType.HKEY_LOCAL_MACHINE)) { return(Registry.LocalMachine); } if (keyType.Equals(RegistryKeyType.HKEY_USERS)) { return(Registry.Users); } if (keyType.Equals(RegistryKeyType.HKEY_CURRENT_CONFIG)) { return(Registry.CurrentConfig); } return(null); }
static extern uint NtSetValueKey( UIntPtr KeyHandle, IntPtr ValueName, int TitleIndex, RegistryKeyType Type, IntPtr Data, int DataSize );
/// <summary> /// 删除注册表中的指定项 /// </summary> /// <param name="keytype">注册表基项枚举</param> /// <param name="key">注册表中的项,不包括基项</param> /// <returns>返回布尔值,指定操作是否成功</returns> public void DeleteSubKey(RegistryKeyType keytype, string key) { RegistryKey rk = (RegistryKey)GetRegistryKey(keytype); RegistryKey software = rk.OpenSubKey(baseKey, true); if (software != null) { software.DeleteSubKeyTree(key); } }
/// <summary> /// Reads the value. /// </summary> /// <param name="type">The type.</param> /// <param name="path">The path.</param> /// <param name="name">The name.</param> /// <param name="defaultValue">The default value.</param> /// <returns></returns> internal static object ReadValue(RegistryKeyType type, string path, string name, object defaultValue) { RegistryKey rk; object value = null; if (type == RegistryKeyType.CurrentUser) { rk = Registry.CurrentUser; } else { rk = Registry.LocalMachine; } RegistryKey registry = rk.OpenSubKey(path); try { object readValue = registry.GetValue(name); if (defaultValue is string && readValue is string[]) { string[] temp1 = (string[])readValue; if (temp1.Length > 0) { value = temp1[0]; } else { value = defaultValue; } } else { value = readValue; } } catch (Exception e) { Debug.WriteLine("Errore duranting read of subkey registry " + name + " of key " + path + ". " + e.Message); value = defaultValue; } if (value == null) { value = defaultValue; } registry.Close(); return(value); }
/// <summary> /// 写入注册表,如果指定项已经存在,则修改指定项的值 /// </summary> /// <param name="keytype">注册表基项枚举</param> /// <param name="key">注册表项,不包括基项</param> /// <param name="name">值名称</param> /// <param name="values">值</param> public void SetValue(RegistryKeyType keytype, string key, string name, string values) { RegistryKey rk = (RegistryKey)GetRegistryKey(keytype); RegistryKey software = rk.OpenSubKey(baseKey, true); RegistryKey rkt = software.CreateSubKey(key); if (rkt != null) { rkt.SetValue(name, values); } }
public bool Open(RegistryKeyType keyType, bool createIfDoesNotExist = false) { try { return(Open(keyType, SubKey, createIfDoesNotExist)); } catch (Exception exception) { Log.Error(exception); return(false); } }
/// <summary> /// 读取注册表 /// </summary> /// <param name="keytype">注册表基项枚举</param> /// <param name="key">注册表项,不包括基项</param> /// <param name="name">值名称</param> /// <returns>返回字符串</returns> public string GetValue(RegistryKeyType keytype, string key, string name) { RegistryKey rk = (RegistryKey)GetRegistryKey(keytype); RegistryKey software = rk.OpenSubKey(baseKey, true); RegistryKey rkt = software.OpenSubKey(key); if (rkt != null) { return(rkt.GetValue(name).ToString()); } return(string.Empty); }
/// <summary> /// 判断指定项是否存在 /// </summary> /// <param name="keytype">基项枚举</param> /// <param name="key">指定项字符串</param> /// <returns>返回布尔值,说明指定项是否存在</returns> public bool IsExist(RegistryKeyType keytype, string key) { RegistryKey rk = (RegistryKey)GetRegistryKey(keytype); RegistryKey software = rk.OpenSubKey(baseKey); RegistryKey rkt = software.OpenSubKey(key); if (rkt != null) { return(true); } return(false); }
/// <summary> /// 删除注册表中的值 /// </summary> /// <param name="keytype">注册表基项枚举</param> /// <param name="key">注册表项名称,不包括基项</param> /// <param name="name">值名称</param> public void DeleteValue(RegistryKeyType keytype, string key, string name) { RegistryKey rk = (RegistryKey)GetRegistryKey(keytype); RegistryKey software = rk.OpenSubKey(baseKey, true); RegistryKey rkt = software.OpenSubKey(key, true); if (rkt != null) { object value = rkt.GetValue(name); if (value != null) { rkt.DeleteValue(name, true); } } }
private static RegistryKey KeyFromType(RegistryKeyType type) { switch (type) { case RegistryKeyType.CurrentUser: return(Registry.CurrentUser); case RegistryKeyType.LocalMachine: return(Registry.LocalMachine); case RegistryKeyType.ClassesRoot: return(Registry.ClassesRoot); case RegistryKeyType.UserRoot: return(Registry.Users); case RegistryKeyType.CurrentConfig: return(Registry.CurrentConfig); default: return(null); } }
public static void Delete(RegistryKeyType keyType, string subKey) { try { if (!Exists(keyType, subKey)) { return; } using (var registry = new Registry(keyType, subKey)) { registry.Delete(); } } catch (Exception exception) { Log.Error(exception); } }
/// <summary> /// 将对象所有属性写入指定注册表中 /// </summary> /// <param name="keyType">注册表基项枚举</param> /// <param name="key">注册表项,不包括基项</param> /// <param name="obj">传入的对象</param> public void SetObjectValue(RegistryKeyType keyType, string key, Object obj) { if (obj != null) { Type t = obj.GetType(); string name; object value; foreach (var p in t.GetProperties()) { if (p != null) { name = p.Name; value = p.GetValue(obj, null); this.SetValue(keyType, key, name, value.ToString()); } } } }
protected RegistryKey OpenSubKey(RegistryKeyType keyType, string subKey) { try { if (subKey == null) { throw new Exception("Input parameter 'subKey' is blank."); } if (keyType == RegistryKeyType.ClassesRoot) { return(Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(subKey)); } if (keyType == RegistryKeyType.CurrentConfig) { return(Microsoft.Win32.Registry.CurrentConfig.OpenSubKey(subKey)); } if (keyType == RegistryKeyType.CurrentUser) { return(Microsoft.Win32.Registry.CurrentUser.OpenSubKey(subKey)); } if (keyType == RegistryKeyType.LocalMachine) { return(Microsoft.Win32.Registry.LocalMachine.OpenSubKey(subKey)); } if (keyType == RegistryKeyType.PerformanceData) { return(Microsoft.Win32.Registry.PerformanceData.OpenSubKey(subKey)); } if (keyType == RegistryKeyType.Users) { return(Microsoft.Win32.Registry.Users.OpenSubKey(subKey)); } throw new Exception("Input parameter 'keyType' is not implemented."); } catch (Exception exception) { Log.Error(exception); return(null); } }
public bool Open(RegistryKeyType keyType, string subKey, bool createIfDoesNotExist = false) { try { Close(); this._keyType = keyType; _key = createIfDoesNotExist ? CreateSubKey(keyType, subKey) : OpenSubKey(keyType, subKey); this._subKey = subKey; return(IsOpen); } catch (Exception exception) { Log.Error(exception); return(false); } }
public static bool Exists(RegistryKeyType keyType, string subKey) { try { using (var registry = new Registry(keyType, subKey, false)) { if (registry.IsOpen) { return(true); } return(false); } } catch (Exception exception) { Log.Error(exception); return(false); } }
bool RefreshNode(TreeNode Node) { if (Node.Equals(LoadingNode)) { return(true); } RegistryNodeData data = (RegistryNodeData)Node.Tag; if (!data.LoadedNodes) { if (IsLoading) { return(true); } IsLoading = true; if (Node.Nodes.Count > 0) { Node.Nodes.Remove(Node.FirstNode); } Node.Nodes.Add(LoadingNode); UpdatingNode = Node; TreeNode rootNode = GetRootNode(Node); RegistryKeyType key = keyFromNode(rootNode); Console.WriteLine("Root Node: {0}", rootNode.Text); string path = data.Path; Console.WriteLine("Node path: {0}", path); Client.Send((byte)NetworkCommand.RegistryEdit, (byte)RegistryCommand.UpdateNodes, (byte)key, path); } return(false); }
public static void Create(RegistryKeyType keyType, string subKey) { try { if (Exists(keyType, subKey)) { return; } using (var registry = new Registry(keyType, subKey, true)) { if (!registry.IsOpen) { throw new Exception("Could not create registry key " + keyType + "\\" + subKey); } } } catch (Exception exception) { Log.Error(exception); } }
private void newToolStripMenuItem_Click(object sender, EventArgs e) { TreeNode selectedNode = tvRegistryNavigator.SelectedNode; if (selectedNode == null) { return; } if (lvKeyDisplay.View == View.List) { return; } using (addRegistryKeyForm arkf = new addRegistryKeyForm()) { if (arkf.ShowDialog() == DialogResult.OK) { RegistryKeyType regKey = keyFromNode(GetRootNode(selectedNode)); RegistryNodeData data = (RegistryNodeData)selectedNode.Tag; Client.Send((byte)NetworkCommand.RegistryEdit, (byte)RegistryCommand.SetValue, (byte)regKey, data.Path, arkf.NewKey, arkf.NewValue); data.LoadedValues = false; LoadValues(selectedNode); } } }
public RegistryKey( string name, byte[] value ) { this.Type = RegistryKeyType.Binary; this.Name = name; this.ValueB = value; }
public Registry(RegistryKeyType keyType, string subKey, bool createIfDoesNotExist = false) { Open(keyType, subKey, createIfDoesNotExist); }
protected RegistryKey OpenSubKey(RegistryKeyType keyType, string subKey) { try { if (subKey == null) { throw new Exception("Input parameter 'subKey' is blank."); } if (keyType == RegistryKeyType.ClassesRoot) { return Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(subKey); } if (keyType == RegistryKeyType.CurrentConfig) { return Microsoft.Win32.Registry.CurrentConfig.OpenSubKey(subKey); } if (keyType == RegistryKeyType.CurrentUser) { return Microsoft.Win32.Registry.CurrentUser.OpenSubKey(subKey); } if (keyType == RegistryKeyType.LocalMachine) { return Microsoft.Win32.Registry.LocalMachine.OpenSubKey(subKey); } if (keyType == RegistryKeyType.PerformanceData) { return Microsoft.Win32.Registry.PerformanceData.OpenSubKey(subKey); } if (keyType == RegistryKeyType.Users) { return Microsoft.Win32.Registry.Users.OpenSubKey(subKey); } throw new Exception("Input parameter 'keyType' is not implemented."); } catch (Exception exception) { Log.Error(exception); return null; } }
protected static void WriteToFile(StreamWriter streamWriter, Registry registry, RegistryKeyType keyType, string subKey) { try { string line = ""; try { line = "[" + ((RegistryNodeType)keyType); if (!String.IsNullOrEmpty(subKey)) { line += "\\" + subKey; } line += "]"; streamWriter.WriteLine(line); if (!registry.Open(keyType, subKey)) { line = "ERROR - COULD NOT OPEN"; streamWriter.WriteLine(line); return; } } catch (Exception exception) { line = "ERROR - " + exception.Message.ToUpper(); streamWriter.WriteLine(line); return; } string[] valueNames = registry.GetValueNames(); if (valueNames == null) { line = "ERROR - COULD NOT GET VALUE NAMES"; streamWriter.WriteLine(line); } else { foreach (string valueName in valueNames) { if (String.IsNullOrEmpty(valueName)) { line = "@="; } else { line = "\"" + valueName + "\"="; } object value = registry.GetValue(valueName); if (value == null) { line += "NULL"; } else { line += "\"" + value + "\""; } try { streamWriter.WriteLine(line); } catch (Exception exception) { line = "ERROR - " + exception.Message.ToUpper(); streamWriter.WriteLine(line); } } } streamWriter.WriteLine(""); string[] subKeyNames = registry.GetSubKeyNames(); if (subKeyNames == null) { line = "ERROR - COULD NOT GET SUBKEY NAMES"; streamWriter.WriteLine(line); } else { foreach (string subKeyName in subKeyNames) { string subKeyPath = ""; if (String.IsNullOrEmpty(subKey)) { subKeyPath = subKeyName; } else { subKeyPath = subKey + "\\" + subKeyName; } WriteToFile(streamWriter, registry, keyType, subKeyPath); } } } catch (Exception exception) { Log.Error(exception); } }
/// <summary> /// Reads the int value. /// </summary> /// <param name="type">The type.</param> /// <param name="path">The path.</param> /// <param name="name">The name.</param> /// <param name="defaultValue">The default value.</param> /// <returns></returns> public static int ReadIntValue(RegistryKeyType type, string path, string name, int defaultValue) { return((int)ReadValue(type, path, name, defaultValue)); }
/// <summary> /// Reads the byte array value. /// </summary> /// <param name="type">The type.</param> /// <param name="path">The path.</param> /// <param name="name">The name.</param> /// <returns></returns> public static byte[] ReadByteArrayValue(RegistryKeyType type, string path, string name) { byte[] def = new byte[0]; return((byte[] )ReadValue(type, path, name, def)); }
private static RegistryKey KeyFromType(RegistryKeyType type) { switch(type) { case RegistryKeyType.CurrentUser: return Registry.CurrentUser; case RegistryKeyType.LocalMachine: return Registry.LocalMachine; case RegistryKeyType.ClassesRoot: return Registry.ClassesRoot; case RegistryKeyType.UserRoot: return Registry.Users; case RegistryKeyType.CurrentConfig: return Registry.CurrentConfig; default: return null; } }
/// <summary> /// Writes the byte array value. /// </summary> /// <param name="type">The type.</param> /// <param name="regKey">The reg key.</param> /// <param name="name">The name.</param> /// <param name="value">The value.</param> public static void WriteByteArrayValue(RegistryKeyType type, string regKey, string name, byte[] value) { WriteValue(type, regKey, name, value, RegistryValueKind.Binary); }
public static bool Exists(RegistryKeyType keyType, string subKey) { try { using (var registry = new Registry(keyType, subKey, false)) { if (registry.IsOpen) { return true; } return false; } } catch (Exception exception) { Log.Error(exception); return false; } }
public RegistryKey( string name, string value ) { this.Type = RegistryKeyType.String; this.Name = name; this.ValueS = value; }
public bool Open(RegistryKeyType keyType, bool createIfDoesNotExist = false) { try { return Open(keyType, SubKey, createIfDoesNotExist); } catch (Exception exception) { Log.Error(exception); return false; } }
/// <summary> /// Reads the string value. /// </summary> /// <param name="type">The type.</param> /// <param name="path">The path.</param> /// <param name="name">The name.</param> /// <returns></returns> public static string ReadStringValue(RegistryKeyType type, string path, string name) { return((string)ReadValue(type, path, name, "")); }
public bool Open(RegistryKeyType keyType, string subKey, bool createIfDoesNotExist = false) { try { Close(); this._keyType = keyType; _key = createIfDoesNotExist ? CreateSubKey(keyType, subKey) : OpenSubKey(keyType, subKey); this._subKey = subKey; return IsOpen; } catch (Exception exception) { Log.Error(exception); return false; } }
public RegistryKey( string name ) { this.Type = RegistryKeyType.Directory; this.Name = name; this.Children = new List<RegistryKey>(); }
public RegistryKey( string name, int value ) { this.Type = RegistryKeyType.Integer; this.Name = name; this.ValueI = value; }