📄 win32keyprovider.cs
字号:
{ return defaultValue; } } else { return defaultValue; } } // Decode the value into something that we can return. switch(type) { case REG_BINARY: return data; case REG_DWORD_LITTLE_ENDIAN: { if(data.Length == 4) { return ((int)(data[0])) | (((int)(data[1])) << 8) | (((int)(data[2])) << 16) | (((int)(data[3])) << 24); } } break; case REG_DWORD_BIG_ENDIAN: { if(data.Length == 4) { return ((int)(data[3])) | (((int)(data[2])) << 8) | (((int)(data[1])) << 16) | (((int)(data[0])) << 24); } } break; case REG_SZ: case REG_EXPAND_SZ: { return ArrayToString(data); } // Not reached. case REG_MULTI_SZ: { return ArrayToStringArray(data); } // Not reached. case REG_QWORD_LITTLE_ENDIAN: { if(data.Length == 8) { return ((long)(data[0])) | (((long)(data[1])) << 8) | (((long)(data[2])) << 16) | (((long)(data[3])) << 24) | (((long)(data[4])) << 32) | (((long)(data[5])) << 40) | (((long)(data[6])) << 48) | (((long)(data[7])) << 56); } } break; default: break; } // If we get here, then we don't know how to decode the data. return defaultValue; } // Get the names of all values underneath this registry key. public String[] GetValueNames() { lock(this) { if(hKey != IntPtr.Zero) { // Get the number of value names under the key. uint numSubKeys, numValues; RegQueryInfoKey(hKey, null, IntPtr.Zero, IntPtr.Zero, out numSubKeys, IntPtr.Zero, IntPtr.Zero, out numValues, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); // Create an array to hold the names. String[] names = new String [numValues]; // Enumerate the names into the array. uint index = 0; char[] name = new char [1024]; uint nameLen; while(index < numValues) { name.Initialize(); nameLen = (uint)(name.Length); if(RegEnumValue(hKey, index, name, ref nameLen, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) != 0) { break; } names[(int)(index++)] = ArrayToString(name); } // Return the final name array to the caller. return names; } return new String [0]; } } // Open a subkey. public IRegistryKeyProvider OpenSubKey(String name, bool writable) { lock(this) { if(hKey != IntPtr.Zero) { IntPtr newKey; if(RegOpenKeyEx (hKey, name, 0, (writable ? KEY_ALL_ACCESS : KEY_READ), out newKey) != 0) { return null; } return new Win32KeyProvider (this.name + "\\" + name, newKey); } else { return null; } } } // Set a value under this registry key. public void SetValue(String name, Object value) { uint type; byte[] data; // Convert the value into a byte array and type. if(value is String) { // Convert a string. type = REG_SZ; data = StringToArray((String)value); } else if(value is int) { // Convert a signed integer. int ivalue = (int)value; type = REG_DWORD_LITTLE_ENDIAN; data = new byte [4]; data[0] = (byte)ivalue; data[1] = (byte)(ivalue >> 8); data[2] = (byte)(ivalue >> 16); data[3] = (byte)(ivalue >> 24); } else if(value is uint) { // Convert an unsigned integer. uint uivalue = (uint)value; type = REG_DWORD_LITTLE_ENDIAN; data = new byte [4]; data[0] = (byte)uivalue; data[1] = (byte)(uivalue >> 8); data[2] = (byte)(uivalue >> 16); data[3] = (byte)(uivalue >> 24); } else if(value is long) { // Convert a signed long integer. long lvalue = (long)value; type = REG_QWORD_LITTLE_ENDIAN; data = new byte [8]; data[0] = (byte)lvalue; data[1] = (byte)(lvalue >> 8); data[2] = (byte)(lvalue >> 16); data[3] = (byte)(lvalue >> 24); data[4] = (byte)(lvalue >> 32); data[5] = (byte)(lvalue >> 40); data[6] = (byte)(lvalue >> 48); data[7] = (byte)(lvalue >> 56); } else if(value is ulong) { // Convert an unsigned long integer. ulong ulvalue = (ulong)value; type = REG_QWORD_LITTLE_ENDIAN; data = new byte [8]; data[0] = (byte)ulvalue; data[1] = (byte)(ulvalue >> 8); data[2] = (byte)(ulvalue >> 16); data[3] = (byte)(ulvalue >> 24); data[4] = (byte)(ulvalue >> 32); data[5] = (byte)(ulvalue >> 40); data[6] = (byte)(ulvalue >> 48); data[7] = (byte)(ulvalue >> 56); } else if(value is byte[]) { // Convert a raw binary byte array. type = REG_BINARY; data = (byte[])value; } else if(value is String[]) { // Convert an array of strings. type = REG_MULTI_SZ; data = StringArrayToArray((String[])value); } else { // Last ditch attempt: use the string form of the value. type = REG_SZ; data = StringToArray(value.ToString()); } // Set the value within the registry. lock(this) { if(hKey != IntPtr.Zero) { RegSetValueEx(hKey, name, 0, type, data, (uint)(data.Length)); } } } // Get the name of this registry key provider. public String Name { get { return name; } } // Get the number of subkeys underneath this key. public int SubKeyCount { get { lock(this) { if(hKey != IntPtr.Zero) { uint numSubKeys, numValues; RegQueryInfoKey(hKey, null, IntPtr.Zero, IntPtr.Zero, out numSubKeys, IntPtr.Zero, IntPtr.Zero, out numValues, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); return (int)numSubKeys; } return 0; } } } // Get the number of values that are associated with this key. public int ValueCount { get { lock(this) { if(hKey != IntPtr.Zero) { uint numSubKeys, numValues; RegQueryInfoKey(hKey, null, IntPtr.Zero, IntPtr.Zero, out numSubKeys, IntPtr.Zero, IntPtr.Zero, out numValues, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); return (int)numValues; } return 0; } } } // Convert a hive value into a HKEY value. public static IntPtr HiveToHKey(RegistryHive hive) { return new IntPtr((int)hive); } // Import the Win32 registry functions from "advapi32.dll". [DllImport("advapi32.dll", CallingConvention=CallingConvention.Winapi)] [MethodImpl(MethodImplOptions.PreserveSig)] extern private static int RegCloseKey(IntPtr hKey); [DllImport("advapi32.dll", EntryPoint="RegConnectRegistryW", CallingConvention=CallingConvention.Winapi)] [MethodImpl(MethodImplOptions.PreserveSig)] extern public static int RegConnectRegistry ([MarshalAs(UnmanagedType.LPWStr)] String lpMachineName, IntPtr hKey, out IntPtr phkResult); [DllImport("advapi32.dll", EntryPoint="RegCreateKeyW", CallingConvention=CallingConvention.Winapi)] [MethodImpl(MethodImplOptions.PreserveSig)] extern private static int RegCreateKey (IntPtr hkey, [MarshalAs(UnmanagedType.LPWStr)] String lpSubKey, out IntPtr phkResult); [DllImport("advapi32.dll", EntryPoint="RegDeleteKeyW", CallingConvention=CallingConvention.Winapi)] [MethodImpl(MethodImplOptions.PreserveSig)] extern private static int RegDeleteKey (IntPtr hkey, [MarshalAs(UnmanagedType.LPWStr)] String lpSubKey); [DllImport("advapi32.dll", EntryPoint="RegDeleteValueW", CallingConvention=CallingConvention.Winapi)] [MethodImpl(MethodImplOptions.PreserveSig)] extern private static int RegDeleteValue (IntPtr hkey, [MarshalAs(UnmanagedType.LPWStr)] String lpValueName); [DllImport("advapi32.dll", EntryPoint="RegEnumKeyExW", CallingConvention=CallingConvention.Winapi)] [MethodImpl(MethodImplOptions.PreserveSig)] extern private static int RegEnumKeyEx (IntPtr hkey, uint index, char[] lpName, ref uint lpcbName, IntPtr reserved, IntPtr lpClass, IntPtr lpcbClass, out long lpftLastWriteTime); [DllImport("advapi32.dll", EntryPoint="RegEnumValueW", CallingConvention=CallingConvention.Winapi)] [MethodImpl(MethodImplOptions.PreserveSig)] extern private static int RegEnumValue (IntPtr hkey, uint index, char[] lpValueName, ref uint lpcbValueName, IntPtr reserved, IntPtr lpType, IntPtr lpData, IntPtr lpcbData); [DllImport("advapi32.dll", CallingConvention=CallingConvention.Winapi)] [MethodImpl(MethodImplOptions.PreserveSig)] extern private static int RegFlushKey(IntPtr hKey); [DllImport("advapi32.dll", EntryPoint="RegOpenKeyExW", CallingConvention=CallingConvention.Winapi)] [MethodImpl(MethodImplOptions.PreserveSig)] extern private static int RegOpenKeyEx (IntPtr hkey, [MarshalAs(UnmanagedType.LPWStr)] String lpSubKey, uint ulOptions, uint samDesired, out IntPtr phkResult); [DllImport("advapi32.dll", EntryPoint="RegQueryInfoKeyW", CallingConvention=CallingConvention.Winapi)] [MethodImpl(MethodImplOptions.PreserveSig)] extern private static int RegQueryInfoKey (IntPtr hkey, byte[] lpClass, IntPtr lpcbClass, IntPtr lpReserved, out uint lpcSubKeys, IntPtr lpcbMaxSubKeyLen, IntPtr lpcbMaxClassLen, out uint lpcValues, IntPtr lpcbMaxValueNameLen, IntPtr lpcbMaxValueLen, IntPtr lpcbSecurityDescriptor, IntPtr lpftLastWriteTime); [DllImport("advapi32.dll", EntryPoint="RegQueryValueExW", CallingConvention=CallingConvention.Winapi)] [MethodImpl(MethodImplOptions.PreserveSig)] extern private static int RegQueryValueEx (IntPtr hkey, [MarshalAs(UnmanagedType.LPWStr)] String lpValueName, IntPtr lpReserved, out uint lpType, byte[] lpData, ref uint lpcbData); [DllImport("advapi32.dll", EntryPoint="RegQueryValueExW", CallingConvention=CallingConvention.Winapi)] [MethodImpl(MethodImplOptions.PreserveSig)] extern private static int RegQueryValueEx (IntPtr hkey, [MarshalAs(UnmanagedType.LPWStr)] String lpValueName, IntPtr lpReserved, out uint lpType, IntPtr lpData, ref uint lpcbData); [DllImport("advapi32.dll", EntryPoint="RegSetValueExW", CallingConvention=CallingConvention.Winapi)] [MethodImpl(MethodImplOptions.PreserveSig)] extern private static int RegSetValueEx (IntPtr hkey, [MarshalAs(UnmanagedType.LPWStr)] String lpValueName, uint Reserved, uint dwType, byte[] lpData, uint cbData); // Type codes for values that can be stored in the registry. private const uint REG_NONE = 0; private const uint REG_SZ = 1; private const uint REG_EXPAND_SZ = 2; private const uint REG_BINARY = 3; private const uint REG_DWORD_LITTLE_ENDIAN = 4; private const uint REG_DWORD_BIG_ENDIAN = 5; private const uint REG_LINK = 6; private const uint REG_MULTI_SZ = 7; private const uint REG_RESOURCE_LIST = 8; private const uint REG_FULL_RESOURCE_DESCRIPTOR = 9; private const uint REG_RESOURCE_REQUIREMENTS_LIST = 10; private const uint REG_QWORD_LITTLE_ENDIAN = 11; // Access types for the "samDesired" parameter of "RegOpenKeyEx". private const uint KEY_QUERY_VALUE = 1; private const uint KEY_SET_VALUE = 2; private const uint KEY_CREATE_SUB_KEY = 4; private const uint KEY_ENUMERATE_SUB_KEYS = 8; private const uint KEY_NOTIFY = 16; private const uint KEY_CREATE_LINK = 32; private const uint KEY_WRITE = 0x20006; private const uint KEY_READ = 0x20019; private const uint KEY_ALL_ACCESS = 0xF003F;}; // class Win32KeyProvider#endif // CONFIG_WIN32_SPECIFICS}; // namespace Microsoft.Win32
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -