RegistryKey::OpenRemoteBaseKey Method (RegistryHive, String^)

 

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
)

Parameters

hKey
Type: Microsoft.Win32::RegistryHive

The HKEY to open, from the RegistryHive enumeration.

machineName
Type: System::String^

The remote machine.

Return Value

Type: Microsoft.Win32::RegistryKey^

The requested registry key.

Exception Condition
ArgumentException

hKey is invalid.

IOException

machineName is not found.

ArgumentNullException

machineName is null.

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 namespace System;
using namespace System::IO;
using namespace System::Security::Permissions;
using namespace Microsoft::Win32;


int main( int argc, char *argv[] )
{
   RegistryKey ^ environmentKey;

   // Check that an argument was specified when the 
   // program was invoked.
   if ( argc == 1 )
   {
      Console::WriteLine( "Error: The name of the remote computer "
      "must be specified as input on the command line." );
      return  -1;
   }

   try
   {

      // Open HKEY_CURRENT_USER\Environment on a remote computer.
      environmentKey = RegistryKey::OpenRemoteBaseKey( RegistryHive::CurrentUser, gcnew String(argv[ 1 ]) )->OpenSubKey( "Environment" );
   }
   catch ( IOException^ e ) 
   {
      Console::WriteLine(  "{0}: {1}", e->GetType()->Name, e->Message );
      return  -1;
   }


   // Print the values.
   Console::WriteLine( "\nThere are {0} values for {1}.", environmentKey->ValueCount.ToString(), environmentKey->Name );
   array<String^>^valueNames = environmentKey->GetValueNames();
   for ( int i = 0; i < environmentKey->ValueCount; i++ )
   {
      Console::WriteLine(  "{0,-20}: {1}", valueNames[ i ], environmentKey->GetValue( valueNames[ i ] )->ToString() );

   }

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

SecurityPermission

for the ability to access the specified registry key if it is a remote key. Associated enumeration: SecurityPermissionFlag::UnmanagedCode

.NET Framework
Available since 1.1
Return to top
Show: