📄 registry.cs
字号:
}
}
else
{
throw new ObjectDisposedException("The RegistryKey being manipulated is closed (closed keys cannot be accessed).");
}
}
#endregion
#region Delete SubKey
/// <summary>
/// Deletes the specified subkey. The string subkey is not case-sensitive.
/// </summary>
/// <param name="subkey">Name of the subkey to delete.</param>
/// <exception cref="System.ArgumentException">The specified subkey is not a valid reference to a registry key.</exception>
/// <exception cref="System.ArgumentNullException">The subkey is null.</exception>
/// <exception cref="System.ObjectDisposedException">The RegistryKey on which this method is being invoked is closed (closed keys cannot be accessed).</exception>
public void DeleteSubKey(string subkey)
{
DeleteSubKey(subkey, true);
}
/// <summary>
/// Deletes the specified subkey. The string subkey is not case-sensitive.
/// </summary>
/// <param name="subkey">Name of the subkey to delete.</param>
/// <param name="throwOnMissingSubKey">Indicates whether an exception should be raised if the specified subkey cannot be found.
/// If this argument is true and the specified subkey does not exist then an exception is raised.
/// If this argument is false and the specified subkey does not exist, then no action is taken</param>
/// <exception cref="System.ArgumentException">The specified subkey is not a valid reference to a registry key (and throwOnMissingSubKey is true).</exception>
/// <exception cref="System.ArgumentNullException">The subkey is null.</exception>
/// <exception cref="System.ObjectDisposedException">The RegistryKey on which this method is being invoked is closed (closed keys cannot be accessed).</exception>
public void DeleteSubKey(string subkey, bool throwOnMissingSubKey)
{
if(subkey==null || subkey=="")
{
throw new ArgumentNullException("The subkey is null");
}
else
{
if(CheckHKey())
{
//delete the subkey
int result = RegDeleteKey(m_handle, subkey);
//if operation failed
if(result != 0)
{
//only throw if flag was set
if(throwOnMissingSubKey)
{
throw new ArgumentException("The specified subkey is not a valid reference to a registry key");
}
}
}
else
{
//key is closed
throw new ObjectDisposedException("The RegistryKey on which this method is being invoked is closed (closed keys cannot be accessed).");
}
}
}
#endregion
#region Delete SubKey Tree
/// <summary>
/// Deletes a subkey and any child subkeys recursively.
/// The string subKey is not case-sensitive.
/// </summary>
/// <param name="subkey">Subkey to delete.</param>
/// <exception cref="System.ArgumentNullException">The subkey parameter is null.</exception>
/// <exception cref="System.ArgumentException">Deletion of a root hive is attempted.
/// The subkey parameter does not match a valid registry subkey.</exception>
/// <exception cref="System.ObjectDisposedException">The RegistryKey being manipulated is closed (closed keys cannot be accessed).</exception>
public void DeleteSubKeyTree(string subkey)
{
//call delete subkey - this will delete all sub keys autmoatically
DeleteSubKey(subkey, true);
}
#endregion
#region Get SubKey Names
/// <summary>
/// Retrieves an array of strings that contains all the subkey names.
/// </summary>
/// <returns>An array of strings that contains the names of the subkeys for the current key.</returns>
/// <exception cref="System.ObjectDisposedException">The RegistryKey being manipulated is closed (closed keys cannot be accessed).</exception>
public string[] GetSubKeyNames()
{
if(CheckHKey())
{
//error/success returned by RegKeyEnumEx
int result = 0;
//store the names
System.Collections.ArrayList subkeynames = new System.Collections.ArrayList();
int index = 0;
//buffer to store the name
char[] buffer = new char[256];
int keynamelen = buffer.Length;
//enumerate sub keys
result = RegEnumKeyEx(m_handle, index, buffer, ref keynamelen, 0, null, 0, 0);
//while there are more key names available
while(result != ERROR_NO_MORE_ITEMS)
{
//add the name to the arraylist
subkeynames.Add(new string(buffer, 0, keynamelen));
//increment index
index++;
//reset length available to max
keynamelen = buffer.Length;
//retrieve next key name
result = RegEnumKeyEx(m_handle, index, buffer, ref keynamelen, 0, null, 0, 0);
}
//sort the results
subkeynames.Sort();
//return a fixed size string array
return (string[])subkeynames.ToArray(typeof(string));
}
else
{
throw new ObjectDisposedException("The RegistryKey being manipulated is closed (closed keys cannot be accessed).");
}
}
#endregion
#region SubKey Count
/// <summary>
/// Retrieves the count of subkeys at the base level, for the current key.
/// </summary>
/// <exception cref="System.ObjectDisposedException"> The RegistryKey being manipulated is closed (closed keys cannot be accessed).</exception>
public int SubKeyCount
{
get
{
//check handle
if(CheckHKey())
{
int subkeycount;
int valuescount;
int maxsubkeylen;
int maxsubkeyclasslen;
int maxvalnamelen;
int maxvallen;
char[] name = new char[256];
int namelen = name.Length;
if(RegQueryInfoKey(m_handle, name, ref namelen, 0, out subkeycount, out maxsubkeylen, out maxsubkeyclasslen, out valuescount, out maxvalnamelen, out maxvallen, 0, 0)==0)
{
return subkeycount;
}
else
{
throw new ExternalException("Error retrieving registry properties");
}
}
else
{
throw new ObjectDisposedException("The RegistryKey being manipulated is closed (closed keys cannot be accessed).");
}
}
}
#endregion
#region Get Value
/// <summary>
/// Retrieves the data associated with the specified value, or null if the value does not exist.
/// </summary>
/// <param name="name">Name of the value to retrieve.</param>
/// <returns>The data associated with name , or null if the value does not exist.</returns>
/// <exception cref="System.ArgumentException">The RegistryKey being manipulated does not exist.</exception>
/// <exception cref="System.ObjectDisposedException">The RegistryKey being manipulated is closed (closed keys cannot be accessed).</exception>
public object GetValue(string name)
{
return GetValue(name, null);
}
/// <summary>
/// Retrieves the specified value, or the default value you provide if the specified value is not found.
/// </summary>
/// <param name="name">Name of the value to retrieve.</param>
/// <param name="defaultValue">Value to return if name does not exist.</param>
/// <returns>The data associated with name, or defaultValue if name is not found.</returns>
/// <exception cref="System.ArgumentException">The RegistryKey being manipulated does not exist.</exception>
/// <exception cref="System.ObjectDisposedException">The RegistryKey being manipulated is closed (closed keys cannot be accessed).</exception>
public object GetValue(string name, object defaultValue)
{
if(CheckHKey())
{
KeyType kt = 0;
//support up to 256 characters (512 bytes)
byte[] buffer;
//pass in buffer size
int size = 0;
//determine validity and get required buffer size
int result = RegQueryValueEx(m_handle, name, 0, ref kt, null, ref size);
//catch value name not valid
if(result==87)
{
return defaultValue;
}
//call api again with valid buffer size
buffer = new byte[size];
result = RegQueryValueEx(m_handle, name, 0, ref kt, buffer, ref size);
//return appropriate type of value
switch(kt)
{
case KeyType.Binary:
//return binary data (byte[])
return buffer;
case KeyType.DWord:
//return value as dword (UInt32)
if (buffer.Length != 4) // If buffer is not 4 bytes, then just return 0.
return (UInt32)0;
return System.BitConverter.ToUInt32(buffer, 0);
case KeyType.ExpandString:
case KeyType.String:
//return value as a string (trailing null removed)
return System.Text.Encoding.Unicode.GetString(buffer, 0, size).TrimEnd('\0');
case KeyType.MultiString:
//get string of value
string raw = System.Text.Encoding.Unicode.GetString(buffer, 0, size).TrimEnd('\0');
//return array of substrings between single nulls
return raw.Split('\0');
default:
return defaultValue;
}
}
else
{
throw new ObjectDisposedException("The RegistryKey being manipulated is closed (closed keys cannot be accessed).");
}
}
#endregion
#region Set Value
/// <summary>
/// Sets the specified value.
/// </summary>
/// <param name="name">Name of value to store data in.</param>
/// <param name="value">Data to store.</param>
/// <exception cref="System.ArgumentException">The length of the specified value is longer than the maximum length allowed (255 characters).</exception>
/// <exception cref="System.ArgumentNullException">value is null.</exception>
/// <exception cref="System.ObjectDisposedException">The RegistryKey being set is closed (closed keys cannot be accessed).</exception>
/// <exception cref="System.UnauthorizedAccessException">The RegistryKey being set is readonly, and cannot be written to (for example, it is a root-level node, or the key has not been opened with write-access).</exception>
public void SetValue(string name, object value)
{
if(m_writable)
{
if(CheckHKey())
{
KeyType type = 0;
byte[] data;
switch(value.GetType().ToString())
{
case "System.String":
type = KeyType.String;
data = System.Text.Encoding.Unicode.GetBytes((string)value + '\0');
break;
case "System.String[]":
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (string str in (string[])value)
sb.Append(str + '\0');
sb.Append('\0'); // terminated by two null characters
type = KeyType.MultiString;
data = System.Text.Encoding.Unicode.GetBytes(sb.ToString());
break;
case "System.Byte[]":
type = KeyType.Binary;
data = (byte[])value;
break;
case "System.Int32":
type = KeyType.DWord;
data = BitConverter.GetBytes((int)value);
break;
case "System.UInt32":
type = KeyType.DWord;
data = BitConverter.GetBytes((uint)value);
break;
default:
throw new ArgumentException("value is not a supported type");
}
int size = data.Length;
int result = RegSetValueEx(m_handle, name, 0, type, data, size);
if(result!=0)
{
throw new ExternalException("Error writing to the RegistryKey");
}
}
else
{
throw new ObjectDisposedException("The RegistryKey being manipulated is closed (closed keys cannot be accessed).");
}
}
else
{
throw new UnauthorizedAccessException("Cannot set value on RegistryKey which was opened as ReadOnly");
}
}
#endregion
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -