📄 registrykey.cs
字号:
//
// Verify the key exists
//
bool found = false;
string[] subs = GetSubKeyNames();
foreach(string sub in subs)
{
if( string.Compare(sub, name, true) == 0 )
{
found = true;
break;
}
}
if( !found && exceptionIfMissing )
{
throw new ArgumentException();
}
//
// Verify the key has no sub-keys
//
RegistryKey subKey = OpenSubKey(name);
if( subKey == null )
{
throw new InvalidOperationException();
}
string child = subKey.GetKey(0);
if( child != null )
{
throw new InvalidOperationException();
}
//
// Delete
//
int ret = NativeRegistry.RegDeleteKey(HKey, name);
Debug.WriteLine(string.Format("RegistryKey.DeleteSubKey returns: {0}", ret));
}
public void DeleteSubKey(string name)
{
DeleteSubKey(name, false);
}
public void DeleteValue(string name, bool exceptionIfMissing)
{
VerifyInput(name);
//
// Verify the value exists
//
bool found = false;
string[] values = GetValueNames();
foreach(string val in values)
{
if( string.Compare(val, name, true) == 0 )
{
found = true;
break;
}
}
if( !found && exceptionIfMissing )
{
throw new ArgumentException();
}
//
// Delete
//
int ret = NativeRegistry.RegDeleteValue(HKey, name);
Debug.WriteLine(string.Format("RegistryKey.RegDeleteValue returns: {0}", ret));
}
public void DeleteValue(string name)
{
DeleteValue(name, false);
}
public void Flush()
{
NativeRegistry.RegFlushKey(HKey);
}
public string[] GetValueNames()
{
if( IsClosed == true )
{
throw new ObjectDisposedException("");
}
System.Collections.ArrayList list = new System.Collections.ArrayList();
string res = "";
int count = 0;
while( res != null )
{
res = GetValue(count);
count++;
if ( res != null )
{
list.Add(res);
}
}
return (string[])list.ToArray(typeof(string));
}
public object GetValue(string name, object byDefault)
{
object obj = GetValue(name);
if( obj == null )
{
return byDefault;
}
else
{
return obj;
}
}
public object GetValue(string name)
{
VerifyInput(name);
int MaxSize = 2 * MAX_LENGTH;
byte[] val = new byte[ MaxSize ];
NativeRegistry.KeyType type = NativeRegistry.KeyType.REG_BINARY;
int ret = NativeRegistry.RegQueryValueEx(
HKey,
name,
0,
ref type,
val,
ref MaxSize);
Debug.WriteLine(string.Format("RegistryKey.RegQueryValueEx returns: {0}", ret));
object toBeReturned = null;
if( ret == NativeRegistry.ERROR_SUCCESS )
{
switch(type)
{
case NativeRegistry.KeyType.REG_BINARY:
byte[] res = new byte[MaxSize];
val.CopyTo(res, MaxSize);
toBeReturned = res;
break;
case NativeRegistry.KeyType.REG_DWORD:
toBeReturned = System.BitConverter.ToUInt32( val, 0 );
break;
case NativeRegistry.KeyType.REG_SZ:
// -1 to remove the \0 included into the count
toBeReturned = System.Text.Encoding.Unicode.GetString( val, 0, MaxSize - 1);
break;
case NativeRegistry.KeyType.REG_EXPAND_SZ:
default:
throw new NotSupportedException();
}
}
return toBeReturned;
}
public override string ToString()
{
return Name + string.Format(" [0x{0}]", HKey.ToString("x"));
}
#region --- Private Methods ---
private string GetKey(int index)
{
int MaxSize = 2 * MAX_LENGTH;
byte[] key = new byte[ MaxSize ];
byte[] Class = new byte[ MaxSize ];
int maxLength = MAX_LENGTH;
int nullInt = MAX_LENGTH;
int ret = NativeRegistry.RegEnumKeyEx(
HKey,
index,
key, ref maxLength,
0, Class, ref nullInt, 0 );
Debug.WriteLine(string.Format("RegistryKey.GetKey returns: {0}", ret));
if( ret == NativeRegistry.ERROR_SUCCESS )
{
// Convert key name and class name to string.
return System.Text.Encoding.Unicode.GetString( key, 0, maxLength * 2 );
}
else
{
return null;
}
}
private string GetValue(int index)
{
int MaxSize = 2 * MAX_LENGTH;
byte[] val = new byte[ MaxSize ];
byte[] notUsed = null;
int maxLength = MAX_LENGTH;
int nullInt = 0;
NativeRegistry.KeyType type = NativeRegistry.KeyType.REG_BINARY;
int ret = NativeRegistry.RegEnumValue(
HKey,
index,
val,
ref maxLength,
0, ref type, ref notUsed, ref nullInt);
Debug.WriteLine(string.Format("RegistryKey.GetValue returns: {0}", ret));
if( ret == NativeRegistry.ERROR_SUCCESS )
{
// Convert key name and class name to string.
return System.Text.Encoding.Unicode.GetString( val, 0, maxLength * 2 );
}
else
{
return null;
}
}
private void VerifyInput(string input)
{
if( input == null)
{
throw new ArgumentNullException();
}
if( input.Length > MAX_LENGTH )
{
throw new ArgumentException();
}
if( IsClosed == true )
{
throw new ObjectDisposedException(input);
}
}
#endregion
#region --- IDisposable Members ---
public void Dispose()
{
Close();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -