Click here to Skip to main content
11,415,000 members (64,576 online)
Click here to Skip to main content

Import/Export registry sections as XML

, 8 Feb 2005
Rate this:
Please Sign up or sign in to vote.
Allows for the import and export of registry sections as XML.

Sample screenshot

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 );

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Sam DenHartog
Web Developer
Mexico Mexico
Private consultant living in sunny Mazatlan, Mexico. I been developing commerical applications for over 15 years in C, C++ and Java. I am very excited about working with C# and web services.

I moved down to Mexico and have been freelancing ever since. Nothing like working on the beach while drinking a margarita... except I keep getting sand in my laptop.

Comments and Discussions

 
QuestionAny change there is an enhancement possible? Pin
Niteowls4-Oct-14 5:23
memberNiteowls4-Oct-14 5:23 
QuestionOkay to modify/rehash? Pin
Member 107355478-Apr-14 23:01
memberMember 107355478-Apr-14 23:01 
SuggestionPerformance issues Pin
Midax14-Nov-12 6:47
memberMidax14-Nov-12 6:47 
GeneralMy vote of 5 Pin
Midax14-Nov-12 6:44
memberMidax14-Nov-12 6:44 
QuestionIndex was outside the bounds of the array. Pin
Funnyboy26-Aug-12 20:10
memberFunnyboy26-Aug-12 20:10 
QuestionRegarding license Pin
ub3rst4r14-Dec-10 21:08
memberub3rst4r14-Dec-10 21:08 
QuestionHow to get rid of the unnecessary null terminators on REG_SZ key values Pin
Mark Hemingway4-Mar-10 1:53
memberMark Hemingway4-Mar-10 1:53 
Generalgiving exception when i tried running the code in vs 2008 Pin
Geetha_Kumari12-Nov-08 1:49
memberGeetha_Kumari12-Nov-08 1:49 
GeneralRe: giving exception when i tried running the code in vs 2008 Pin
gardia19-Aug-09 4:08
membergardia19-Aug-09 4:08 
GeneralRe: giving exception when i tried running the code in vs 2008 Pin
franco nero10-Dec-09 12:27
memberfranco nero10-Dec-09 12:27 
GeneralRe: giving exception when i tried running the code in vs 2008 Pin
boslbosl10-Feb-10 23:27
memberboslbosl10-Feb-10 23:27 
QuestionNot working on windows 64 bit Pin
Dev-Rama11-Jun-07 1:52
memberDev-Rama11-Jun-07 1:52 
GeneralAnother way to import registry in C# Pin
mycsharpcorner6-Apr-07 8:26
membermycsharpcorner6-Apr-07 8:26 
GeneralBugs/Ideas Pin
Eric Engler8-Nov-06 10:55
memberEric Engler8-Nov-06 10:55 
QuestionImport/Export classes in .Net FW 2.0? Pin
D.Ecke20-Sep-06 4:20
memberD.Ecke20-Sep-06 4:20 
AnswerRe: Import/Export classes in .Net FW 2.0? Pin
Zylwee9-Oct-06 2:33
memberZylwee9-Oct-06 2:33 
GeneralGreat Creation Pin
anksbond11-Apr-06 22:35
memberanksbond11-Apr-06 22:35 
GeneralRe: Same article on CP.. Pin
Sam DenHartog8-Feb-06 6:01
memberSam DenHartog8-Feb-06 6:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.4 | Last Updated 8 Feb 2005
Article Copyright 2005 by Sam DenHartog
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid