// Open a registry key for a local or remote machine. public static RegistryKey OpenRemoteBaseKey(RegistryHive hKey, String machineName) { // Validate the parameters. if (hKey < RegistryHive.ClassesRoot || hKey > RegistryHive.DynData) { throw new ArgumentException(_("Arg_InvalidHive")); } if (machineName == null) { throw new ArgumentNullException("machineName"); } // Get the name of the hive to be accessed. String name = hiveNames [((int)hKey) - (int)(RegistryHive.ClassesRoot)]; // Is this a remote hive reference? if (machineName != String.Empty) { if (Win32KeyProvider.IsWin32()) { // Attempt to connect to the remote registry. IntPtr newKey; if (Win32KeyProvider.RegConnectRegistry (machineName, Win32KeyProvider.HiveToHKey(hKey), out newKey) != 0) { throw new SecurityException (_("Invalid_RemoteRegistry")); } return(new RegistryKey (new Win32KeyProvider(name, newKey), true)); } else { // Not Win32 - cannot access remote registries. throw new SecurityException (_("Invalid_RemoteRegistry")); } } // Open a local hive. return(new RegistryKey (Registry.GetProvider(hKey, name), true)); }
// Get a registry key provider for a particular hive. internal static IRegistryKeyProvider GetProvider (RegistryHive hKey, String name) { int index; lock (typeof(Registry)) { // Allocate the "providers" array if necessary. if (providers == null) { providers = new IRegistryKeyProvider[7]; } // See if we already have a provider for this hive. index = ((int)hKey) - ((int)(RegistryHive.ClassesRoot)); if (providers[index] != null) { return(providers[index]); } // Create a Win32 provider if we are on a Windows system. if (Win32KeyProvider.IsWin32()) { providers[index] = new Win32KeyProvider (name, Win32KeyProvider.HiveToHKey(hKey)); return(providers[index]); } // Try to create a file-based provider for the hive. try { providers[index] = new FileKeyProvider(hKey, name); return(providers[index]); } catch (NotSupportedException) { // Could not create the hive directory - fall through. } // Create a memory-based provider on all other systems. providers[index] = new MemoryKeyProvider (null, name, name); return(providers[index]); } }