Click here to Skip to main content
Licence 
First Posted 8 Feb 2005
Views 52,328
Bookmarked 29 times

Import/Export registry sections as XML

By | 8 Feb 2005 | Article
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

About the Author

Sam DenHartog

Web Developer

Mexico Mexico

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionRegarding license Pinmemberub3rst4r20:08 14 Dec '10  
QuestionHow to get rid of the unnecessary null terminators on REG_SZ key values PinmemberMark Hemingway0:53 4 Mar '10  
Generalgiving exception when i tried running the code in vs 2008 PinmemberGeetha_Kumari0:49 12 Nov '08  
GeneralRe: giving exception when i tried running the code in vs 2008 Pinmembergardia3:08 19 Aug '09  
GeneralRe: giving exception when i tried running the code in vs 2008 Pinmemberfranco nero11:27 10 Dec '09  
GeneralRe: giving exception when i tried running the code in vs 2008 Pinmemberboslbosl22:27 10 Feb '10  
QuestionNot working on windows 64 bit PinmemberDev-Rama0:52 11 Jun '07  
GeneralAnother way to import registry in C# Pinmembermycsharpcorner7:26 6 Apr '07  
Just thought I'd contribute, if you want to import a .reg file to the registry in C#, it's very simple... just spawn regedit.exe from the C# process, here is a link about how to do that:
 
http://www.mycsharpcorner.com/Post.aspx?postID=29[^]
 
For more tips, go to: http://www.mycsharpcorner.com

GeneralBugs/Ideas PinmemberEric Engler9:55 8 Nov '06  
QuestionImport/Export classes in .Net FW 2.0? PinmemberD.Ecke3:20 20 Sep '06  
AnswerRe: Import/Export classes in .Net FW 2.0? PinmemberZylwee1:33 9 Oct '06  
GeneralGreat Creation Pinmemberanksbond21:35 11 Apr '06  
GeneralRe: Same article on CP.. PinmemberSam DenHartog5:01 8 Feb '06  

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.

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