
Introduction
If you need to backup and restore sections of the registry using C#, then these classes will be a big help. I have needed to do so in spyware and backup/restore applications for third party products. In C++, I have used the code from Stephane Rodriguez's article here. These classes provide a conversion from the main classes he developed.
Where It Got Tricky
One of the nice things about his routines is that they handle creation of all registry key types. DWORD
with both little and big Endian, links, multi sz, resource lists, etc. I quickly realized that the base registry classes did not handle creation of entries of these types. So I created DllImport
for each of the registry methods needed.
[DllImport("advapi32.dll", EntryPoint="RegOpenKey")] public static extern
int RegOpenKeyA(int hKey, string lpSubKey, ref int phkResult);
[DllImport("advapi32.dll")] public static extern
int RegCloseKey(int hKey);
[DllImport("advapi32.dll", EntryPoint="RegQueryInfoKey")] public static extern
int RegQueryInfoKeyA(int hKey, string lpClass,
ref int lpcbClass, int lpReserved,
ref int lpcSubKeys, ref int lpcbMaxSubKeyLen,
ref int lpcbMaxClassLen, ref int lpcValues,
ref int lpcbMaxValueNameLen, ref int lpcbMaxValueLen,
ref int lpcbSecurityDescriptor,
ref FILETIME lpftLastWriteTime);
[DllImport("advapi32.dll", EntryPoint="RegEnumValue")] public static extern
int RegEnumValueA(int hKey, int dwIndex,
ref byte lpValueName, ref int lpcbValueName,
int lpReserved, ref int lpType, ref byte lpData, ref int lpcbData);
[DllImport("advapi32.dll", EntryPoint="RegEnumKeyEx")] public static extern
int RegEnumKeyExA(int hKey, int dwIndex,
ref byte lpName, ref int lpcbName, int lpReserved,
string lpClass, ref int lpcbClass, ref FILETIME lpftLastWriteTime);
[DllImport("advapi32.dll", EntryPoint="RegSetValueEx")] public static extern
int RegSetValueExA(int hKey, string lpSubKey,
int reserved, int dwType, ref byte lpData, int cbData);
I also changed the UTF8 encoding classes to use C# built in Encoding classes, string routines, etc. I left it using the XML routines he had created as opposed to using the ones built in to C# for sake of time.
Backup A Registry Section
In the end, it provides a very easy way to backup a registry section to a file. You just specify the section (you can use a string pulled from regedit) and then tell it to create the file. This is really great as well as you could load the XML back in and use XPath classes or other tools to analyze it.
string strRegistrySection =
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
if (xmlRegistry.keyExists(strRegistrySection))
{
xmlWriter w = new xmlWriter();
xmlRegistry xmlReg = new xmlRegistry();
w.open( textBoxFileName.Text );
xmlElement wroot = new xmlElement(xmlRegistry.XML_ROOT);
wroot.write(w,1,false,true);
xmlReg.saveAsXml( w, false, strRegistrySection, "");
wroot.writeClosingTag(w,-1,false,true);
w.close();
}
Restore A Registry Section
Restoring a registry section is even easier.
xmlReader xmlReader = new xmlReader();
xmlRegistry xmlReg = new xmlRegistry();
xmlReg.loadAsXml(xmlReader, textBoxFileName.Text );