Remote Registry Backup

C

Chuck B

I am modifying remote registries for about 150 computers. Is there any easy
way in C# to do a remote registry backup?
 
W

Willy Denoyette [MVP]

|
| I am modifying remote registries for about 150 computers. Is there any
easy
| way in C# to do a remote registry backup?
|
|

You'll have to PInvoke the advapi32 API's for this.


Following a code snip to get you started, note that the road to a remote
registry is paved with security checkpoints :) so I suggest you carefully
read the MSDN docs before you dive into this.

...
[DllImport("advapi32")]
static extern int RegConnectRegistry(string machine, UIntPtr hKey, out
IntPtr pRemKey);
[DllImport("advapi32")]
static extern int RegCloseKey(IntPtr hKey);
[DllImport("advapi32")]
static extern int RegSaveKey(IntPtr hKey, string fileout, IntPtr
secdesc);

...
// most important keys, other keys -> winreg.h
const uint HKEY_CLASSES_ROOT = 0x80000000;
const uint HKEY_CURRENT_USER = 0x80000001;
const uint HKEY_LOCAL_MACHINE = 0x80000002;

UIntPtr key = new UIntPtr(HKEY_CLASSES_ROOT);
IntPtr remKey;
int ret = RegConnectRegistry("machineName", key, out remKey);
if(ret == 0) {
int r = RegSaveKey(remKey, "c:\\regRootsave", IntPtr.Zero);
if(r != 0)
Console.WriteLine("Error: {0}", r);
}
if(remKey != IntPtr.Zero)
RegCloseKey(remKey);
...

Willy.
 
E

Eric

Chuck said:
I am modifying remote registries for about 150 computers. Is there any easy
way in C# to do a remote registry backup?

How about something like this:

using Microsoft.Win32;

private void openSubkey(string keyName, string remoteName)
{
RegistryKey regKey;

try
{
// Open registry on a remote computer.
regKey = RegistryKey.OpenRemoteBaseKey(
RegistryHive.LocalMachine, remoteName).OpenSubKey(keyName);
}
catch(IOException ex)
{
printOutput(string.Format("\n{0}: {1}",
ex.GetType().Name, ex.Message));
return;
}

if (regKey != null)
{
...

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

Of course you'll have to be logged-in as an administrator for those
computers.

Eric
 
W

Willy Denoyette [MVP]

I don't think this answers the OP's question!

..... C# to do a remote registry backup?


Willy.

| Chuck B wrote:
|
| > I am modifying remote registries for about 150 computers. Is there any
easy
| > way in C# to do a remote registry backup?
|
| How about something like this:
|
| using Microsoft.Win32;
|
| private void openSubkey(string keyName, string remoteName)
| {
| RegistryKey regKey;
|
| try
| {
| // Open registry on a remote computer.
| regKey = RegistryKey.OpenRemoteBaseKey(
| RegistryHive.LocalMachine, remoteName).OpenSubKey(keyName);
| }
| catch(IOException ex)
| {
| printOutput(string.Format("\n{0}: {1}",
| ex.GetType().Name, ex.Message));
| return;
| }
|
| if (regKey != null)
| {
| ...
|
| // Close the registry key.
| regKey.Close();
| }
| }
|
| Of course you'll have to be logged-in as an administrator for those
| computers.
|
| Eric
|
 
C

Chuck B

Willy Denoyette said:
|
| I am modifying remote registries for about 150 computers. Is there any
easy
| way in C# to do a remote registry backup?
|
|

You'll have to PInvoke the advapi32 API's for this.


Following a code snip to get you started, note that the road to a remote
registry is paved with security checkpoints :) so I suggest you carefully
read the MSDN docs before you dive into this.

..
[DllImport("advapi32")]
static extern int RegConnectRegistry(string machine, UIntPtr hKey, out
IntPtr pRemKey);
[DllImport("advapi32")]
static extern int RegCloseKey(IntPtr hKey);
[DllImport("advapi32")]
static extern int RegSaveKey(IntPtr hKey, string fileout, IntPtr
secdesc);

..
// most important keys, other keys -> winreg.h
const uint HKEY_CLASSES_ROOT = 0x80000000;
const uint HKEY_CURRENT_USER = 0x80000001;
const uint HKEY_LOCAL_MACHINE = 0x80000002;

UIntPtr key = new UIntPtr(HKEY_CLASSES_ROOT);
IntPtr remKey;
int ret = RegConnectRegistry("machineName", key, out remKey);
if(ret == 0) {
int r = RegSaveKey(remKey, "c:\\regRootsave", IntPtr.Zero);
if(r != 0)
Console.WriteLine("Error: {0}", r);
}
if(remKey != IntPtr.Zero)
RegCloseKey(remKey);

Thanks again Willy. Without your help I might be doing this via sneakernet.
Not a fun thought with some 150 computers... :p
 
Joined
Nov 27, 2009
Messages
1
Reaction score
0
[DllImport("advapi32")]

staticexternint RegSaveKey( UIntPtr hKey, string fileout, IntPtr

secdesc);

constuint HKEY_CLASSES_ROOT = 0x80000000;
constuint HKEY_CURRENT_USER = 0x80000001;
constuint HKEY_LOCAL_MACHINE = 0x80000002;
int ret = 0;
UIntPtr key = newUIntPtr(HKEY_CURRENT_USER);
ret = RegSaveKey(key, "E:\\test", IntPtr.Zero);

//Hello can you help me? I wanna backup the xp registry. It writes a 0 kb file.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top