M
Mark
I've had a look around and AFAICT, the Microsoft.Win32.RegistryKey class
doesn't have any members that write the registry to a reg file. So, as I see
it, the options are:
1. Use Microsoft.Win32.RegistryKey to parse each value and write each to a
reg file manually. This is a bit of a pain as I have to write all the code
to keep the syntax of the resultant reg file correct.
2. Shell out and use regedit /e to export the file. Not ideal because when I
do this, I believe that a separate thread is used, so my app carries on
working whilst the registry is still being exported (noticeable for large
exports). OK, I could monitor all this, but again, this seems like really
hard work for such a simple job!
3. Use "advapi32.dll". This is my preferred option (assuming I'm right about
point 1). However, I'm getting errors when running the code:
[DllImport("advapi32.dll", EntryPoint="RegSaveKey")]
public static extern int RegSaveKey(IntPtr hKey, string lpFile, int
lpSecurityAttributes);
IntPtr hKey;
IntPtr hHkcu = new IntPtr((int)RegistryHive.CurrentUser);
int ret = Win32.RegOpenKey(hHkcu, registrykey, out hKey);
if(ret == Win32.SUCCESS)
{
Console.WriteLine("Return code: " + RegSaveKey(hHkcu, @"C:\regbackup.reg",
0).ToString());
}
This compiles fine, and it appears to be trying to do what it's supposed to
do. E.g. if I ensure that the file already exists, I get return code (from
RegOpenKey) 183, which is what the documentation to RegSaveKey says I should
get (file already exists, roughly). So, if I then make sure that the file
doesn't exist, instead of getting return code 0, I get 1314, which seems to
be a permission problem? However, I am an administrator.
If I do the same from the command prompt using regedit, it exports fine.
Any idea?
Cheers
Mark
doesn't have any members that write the registry to a reg file. So, as I see
it, the options are:
1. Use Microsoft.Win32.RegistryKey to parse each value and write each to a
reg file manually. This is a bit of a pain as I have to write all the code
to keep the syntax of the resultant reg file correct.
2. Shell out and use regedit /e to export the file. Not ideal because when I
do this, I believe that a separate thread is used, so my app carries on
working whilst the registry is still being exported (noticeable for large
exports). OK, I could monitor all this, but again, this seems like really
hard work for such a simple job!
3. Use "advapi32.dll". This is my preferred option (assuming I'm right about
point 1). However, I'm getting errors when running the code:
[DllImport("advapi32.dll", EntryPoint="RegSaveKey")]
public static extern int RegSaveKey(IntPtr hKey, string lpFile, int
lpSecurityAttributes);
IntPtr hKey;
IntPtr hHkcu = new IntPtr((int)RegistryHive.CurrentUser);
int ret = Win32.RegOpenKey(hHkcu, registrykey, out hKey);
if(ret == Win32.SUCCESS)
{
Console.WriteLine("Return code: " + RegSaveKey(hHkcu, @"C:\regbackup.reg",
0).ToString());
}
This compiles fine, and it appears to be trying to do what it's supposed to
do. E.g. if I ensure that the file already exists, I get return code (from
RegOpenKey) 183, which is what the documentation to RegSaveKey says I should
get (file already exists, roughly). So, if I then make sure that the file
doesn't exist, instead of getting return code 0, I get 1314, which seems to
be a permission problem? However, I am an administrator.
If I do the same from the command prompt using regedit, it exports fine.
Any idea?
Cheers
Mark