Click here to Skip to main content
11,933,213 members (62,383 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked

Registry Export File (.reg) Parser

, 18 Dec 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
A class to read and parse reg files


You may already - like me some time ago - have needed a possibility to read and to analyze an exported .reg file, in order to compare the given registry values or to import only few of the exported registry values.

In my case, I had to read a reg file of ca. 30 MB size, then to compare all values with the existing registry values and change some of them - some kind of backup/restore solution for poor men Wink | ;-) . I spent some time searching on the Internet for a ready-to-use solution, but all that I found was either another language (C++, VBS) or something not really efficient. So I decided to write my own DLL library to read and parse a reg file.


I already used the .NET Regex class in some projects and was really amazed at how smart the Regex methods have been coded, working much quicker as for example the String class methods. My read function needs less than 5 seconds to read my 30 MB reg file on a standard 3Ghz PC (Windows 7).

The main class is the RegFileObject class.


The raw content of the .reg file is stored in the private field content. The parsed registry values are stored in the RegValues generic dictionary. The dictionary keys are the registry keys found in the .reg file. The dictionary values are again dictionaries with registry values names as dictionary keys and registry values data as dictionary values.

The RegValueObject class diagram:


The main parsing function is the NormalizeDictionary function. It "slices" the raw text content using a given search pattern. It returns the output as a dictionary with found matches as dictionary keys and the whole raw text content between the current match end and next match begin as dictionary value.

/// <summary>
/// Creates a flat Dictionary using given search pattern
/// </summary>
/// <param name="searchPattern">The search pattern</param>
/// <param name="content">The content string to be parsed</param>
/// <param name="stripeBraces">Flag for striping braces 
/// (true for reg keys, false for reg values)</param>
/// <returns>A Dictionary with retrieved keys and remaining content</returns>
private Dictionary<String, String> NormalizeDictionary
    (String searchPattern, String content, bool stripeBraces)
    MatchCollection matches = 
    Regex.Matches(content, searchPattern, RegexOptions.Multiline);
    Int32 startIndex = 0;
    Int32 lengthIndex = 0;
    Dictionary<String, String> dictKeys = new Dictionary<string, string>();
    foreach (Match match in matches)
          //Retrieve key
          String sKey = match.Value;
          if (sKey.EndsWith("\r\n")) sKey = sKey.Substring(0, sKey.Length - 2);
          if (sKey.EndsWith("=")) sKey = sKey.Substring(0, sKey.Length - 1);
          if (stripeBraces) sKey = StripeBraces(sKey);
          if (sKey == "@") 
            sKey = "";
            sKey = StripeLeadingChars(sKey, "\"");
          //Retrieve value
          startIndex = match.Index + match.Length;
          Match nextMatch = match.NextMatch();
          lengthIndex = ((nextMatch.Success) ? 
        nextMatch.Index : content.Length) - startIndex;       
          String sValue = content.Substring(startIndex, lengthIndex);
          //Removing the ending CR
          if (sValue.EndsWith("\r\n")) sValue = sValue.Substring(0, sValue.Length - 2);
          dictKeys.Add(sKey, sValue);
      catch (Exception ex)
          throw new Exception(String.Format
              ("Exception thrown on processing string {0}", match.Value), ex);
    return dictKeys;

The ParseFile function calls the NormalizeDictionary method in two steps: to scan for all registry keys in the first (the search pattern is "^[\t ]*\\[.+\\]\r\n") and to parse registry values for each registry key found in the first step in the second step (search pattern: "^[\t ]*(\".+\"|@)=").


My .reg file parser doesn't support the remove directives (-[HKEY_LOCAL_MACHINE\SOFTWARE\TestSoftware]), as it was not in focus of my project.

I have surely not discovered all issues on parsing f. ex. very "exotic" .reg files, but for my project, it was absolutely sufficient. The simplicity of the hereby presented parsing routines allow however to easily enhance the library by new functions.

Using the Code

The .reg file will be read on creating an instance of the RegFileObject class:

RegFileObject regfile = new RegFileObject(@"C:\Temp\test.reg"); 

The searched registry value can be simply accessed by typing:

RegValueObject tempValue = regfile.RegValues

and the value data by querying the property Value:

String tempData = tempValue.Value; 

or directly:

RegFileObject regfile = new RegFileObject(@"C:\Temp\test.reg");
String tempData = regfile.RegValues

The RegValues Dictionary can be very simply enumerated using the foreach or for statements.


  • 2010, 8th November: Initial release
  • 2011, 14th April: Fixed some typos
  • 2014, 18th December: Fixed multiple empty lines bug (thanks to Jenda27)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Henryk Filipowicz
Software Developer nkt cables GmbH
Germany Germany
MCTS .NET Framework 2.0 Web Applications
MCTS .NET Framework 2.0 Windows Applications
MCTS .NET Framework 2.0 Distributed Applications
MCTS Windows 7, Configuration
MCITP Enterprise Desktop Administrator on Windows 7

You may also be interested in...

Comments and Discussions

Questiongood stuff Pin
Jenda2713-Jun-13 15:32
memberJenda2713-Jun-13 15:32 
AnswerRe: good stuff Pin
Henryk Filipowicz11-Dec-14 2:31
memberHenryk Filipowicz11-Dec-14 2:31 
GeneralRe: good stuff Pin
andrewtheart19-Dec-14 19:14
memberandrewtheart19-Dec-14 19:14 
GeneralRe: good stuff Pin
Henryk Filipowicz12-Feb-15 23:33
memberHenryk Filipowicz12-Feb-15 23:33 
Questionpublishing on my website: Pin
nxexoxn1-Feb-12 21:44
membernxexoxn1-Feb-12 21:44 
AnswerRe: publishing on my website: Pin
Henryk Filipowicz22-Apr-14 5:31
memberHenryk Filipowicz22-Apr-14 5:31 
AnswerRe: publishing on my website: Pin
Zoltán Zörgő18-Dec-14 4:48
mvpZoltán Zörgő18-Dec-14 4:48 
GeneralRe: publishing on my website: Pin
Henryk Filipowicz18-Dec-14 5:09
memberHenryk Filipowicz18-Dec-14 5:09 
Questioninterating Pin
Justin Sunseri22-Nov-10 7:23
memberJustin Sunseri22-Nov-10 7:23 
AnswerRe: interating Pin
Henryk Filipowicz16-Mar-11 7:29
memberHenryk Filipowicz16-Mar-11 7:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web04 | 2.8.151126.1 | Last Updated 18 Dec 2014
Article Copyright 2010 by Henryk Filipowicz
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid