By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,050 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,050 IT Pros & Developers. It's quick & easy.

Remote Registry Backup

P: n/a

I am modifying remote registries for about 150 computers. Is there any easy
way in C# to do a remote registry backup?
Sep 22 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Chuck B" <ch****@shc1.comwrote in message
news:eR**************@TK2MSFTNGP06.phx.gbl...
|
| 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.

Sep 22 '06 #2

P: n/a
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

Sep 22 '06 #3

P: n/a
I don't think this answers the OP's question!

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

"Eric" <en*********@yahoo.comwrote in message
news:11*********************@d34g2000cwd.googlegro ups.com...
| 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
|
Sep 22 '06 #4

P: n/a

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:um*************@TK2MSFTNGP06.phx.gbl...
>
"Chuck B" <ch****@shc1.comwrote in message
news:eR**************@TK2MSFTNGP06.phx.gbl...
|
| 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
Sep 25 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.