RegistryKey.OpenRemoteBaseKey Method

Opens a new RegistryKey that represents the requested key on a remote machine.

Namespace: Microsoft.Win32
Assembly: mscorlib (in mscorlib.dll)

public static RegistryKey OpenRemoteBaseKey (
	RegistryHive hKey,
	string machineName
)
public static RegistryKey OpenRemoteBaseKey (
	RegistryHive hKey, 
	String machineName
)
public static function OpenRemoteBaseKey (
	hKey : RegistryHive, 
	machineName : String
) : RegistryKey
Not applicable.

Parameters

hKey

The HKEY to open, from the RegistryHive enumeration.

machineName

The remote machine.

Return Value

The requested RegistryKey.

Exception typeCondition

ArgumentException

hKey is invalid.

IOException

machineName is not found.

ArgumentNullException

machineName is a null reference (Nothing in Visual Basic).

SecurityException

The user does not have the proper permissions to perform this operation.

UnauthorizedAccessException

The user does not have the necessary registry rights.

The local machine registry is opened if machineName is String.Empty. The requested key must be a root key on the remote machine, and is identified by the appropriate RegistryHive value.

In order for a key to be opened remotely, both the server and client machines must be running the remote registry service, and have remote administration enabled.

The following code example shows how to open a registry key on a remote computer and enumerate the values of the key. The remote computer must be running the remote registry service. Specify the name of the remote computer as a command-line argument when invoking the program.

using System;
using System.IO;
using System.Security.Permissions;
using Microsoft.Win32;

[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum,
    Read = @"HKEY_CURRENT_USER\Environment")]
[assembly: SecurityPermissionAttribute(SecurityAction.RequestMinimum,
    UnmanagedCode = true)]

class RemoteKey
{
    static void Main(string[] args)
    {
        RegistryKey environmentKey;
        string remoteName;

        // Check that an argument was specified when the 
        // program was invoked.
        if(args.Length == 0)
        {
            Console.WriteLine("Error: The name of the remote " +
                "computer must be specified when the program is " +
                "invoked.");
            return;
        }
        else
        {
            remoteName = args[0];
        }

        try
        {
            // Open HKEY_CURRENT_USER\Environment 
            // on a remote computer.
            environmentKey = RegistryKey.OpenRemoteBaseKey(
                RegistryHive.CurrentUser, remoteName).OpenSubKey(
                "Environment");
        }
        catch(IOException e)
        {
            Console.WriteLine("{0}: {1}", 
                e.GetType().Name, e.Message);
            return;
        }

        // Print the values.
        Console.WriteLine("\nThere are {0} values for {1}.", 
            environmentKey.ValueCount.ToString(), 
            environmentKey.Name);
        foreach(string valueName in environmentKey.GetValueNames())
        {
            Console.WriteLine("{0,-20}: {1}", valueName, 
                environmentKey.GetValue(valueName).ToString());
        }

        // Close the registry key.
        environmentKey.Close();
    }
}

import System.*;
import System.IO.*;
import System.Security.Permissions.*;
import Microsoft.Win32.*;

/** @assembly RegistryPermissionAttribute(SecurityAction.RequestMinimum, 
    Read = "HKEY_CURRENT_USER\\Environment")
 */
/** @assembly SecurityPermissionAttribute(SecurityAction.RequestMinimum, 
    UnmanagedCode = true)
 */
class RemoteKey
{
    public static void main(String[] args)
    {
        RegistryKey environmentKey;
        String remoteName;
        // Check that an argument was specified when the 
        // program was invoked.
        if (args.get_Length() == 0) {
            Console.WriteLine("Error: The name of the remote " 
                + "computer must be specified when the program is " 
                + "invoked.");
            return;
        }
        else {
            remoteName = args[0];
        }

        try {
            // Open HKEY_CURRENT_USER\Environment 
            // on a remote computer.
            environmentKey = RegistryKey.OpenRemoteBaseKey(
                RegistryHive.CurrentUser, remoteName).OpenSubKey("Environment");
        }
        catch (IOException e) {
            Console.WriteLine("{0}: {1}", e.GetType().get_Name(), 
                e.get_Message());
            return;
        }

        // Print the values.
        Console.WriteLine("\nThere are {0} values for {1}.", 
            Convert.ToString(environmentKey.get_ValueCount()), 
            environmentKey.get_Name());
        for (int iCtr = 0; iCtr < environmentKey.GetValueNames().get_Length();
            iCtr++) {
            String valueName = (String)environmentKey.GetValueNames().
                get_Item(iCtr);
            Console.WriteLine("{0,-20}: {1}", valueName, 
                environmentKey.GetValue(valueName).ToString());
        }

        // Close the registry key.
        environmentKey.Close();
    } //main
} //RemoteKey

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show: