A Registry Viewer in C#

March 24, 2009 | By | 8 Comments

RegistryViewer

The Registry is a database in Windows that is available for application developers to use for storing small amounts of data. It is mostly used to save application setting like the window size and database connection string. In this series of post I will demonstrate how to create a simple registry editor in C#.

The Registry consists of 5 root keys. Each root key has sub keys that are organized in a CompanyName\SoftwareName\VersionNumber hierarchy. And in each sub key a value could be inserted with some data in it. The root keys and their .NET classes are:

  • HKEY_CLASSES_ROOT (ClassesRoot)
  • HKEY_CURRENT_CONFIG (CurrentConfig)
  • HKEY_CURRENT_USER (CurrentUser)
  • HKEY_LOCAL_MACHINE (LocalMachine)
  • HKEY_USERS (Users)

The 2 classes needed to work with the Registry are Microsoft.Win32.RegistryKey and Microsoft.Win32.Registry, they could be used to manipulate most of the sub keys, but all of root keys are read only. The Microsoft.Win32.RegistryKey object is used to manage the sub keys, and the Microsoft.Win32.Registry is used to read the root keys. The following code reads the entire first and second level of the sub keys in all the root keys that are available to the current user:

The fastest way to read the values and data in a sub key is to use the GetValueNames and GetValue methods of the RegistyKey object. The GetValuesAndData function below takes 2 arguments, a RegistryKey object and a TreeNode object. It fills the TreeNode object with values in the RegistryKey object in a ‘Value: Data’ format. The data is also truncated if it is greater than 50 characters.

The array of strings called values is set to the list of Registry values in the sub key, and a foreach loops through these values. In each iteration of the loop the data of the current value is retrieved using the GetValue function of the RegistryKey object.

Also be sure to check out the free and open source SMARegisTry Backup by Eric Arnol-Martin. It uses this code to backup selective registry keys.

 

Download Source Code

 

Filed in: WinForms | Tags:

Comments (8)

Links to this Post

  1. DotNetShoutout | March 29, 2009
  1. How would i check to see if the data is in a default value?

    if default value dosnt have any data ok if not would you like to remove it.

    http://www.summeylabs.com/images/reg.jpg

    thats what im trying to check

    my if statement dosnt work if i have data in that value it still say its empty etc…

    thank you for your time.
    below is my code.

    static void Main(string[] args)
    {

    RegistryKey MyReg = Registry.LocalMachine.OpenSubKey
    (“SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\setup.exe”, true);

    string IsVal = (string)MyReg.GetValue(“Default”,””);

    if (MyReg.GetValue(IsVal) != null)
    {
    Console.WriteLine(“empty”);

    MyReg.Flush();

    MyReg.Close();

    }

    else

    {

    Console.WriteLine(MyReg.GetValue(IsVal).ToString());
    Console.WriteLine(“removing value now”);

    MyReg.SetValue(“”,”DefaultValue”);
    MyReg.Flush();
    MyReg.Close();

    }

  2. Hi Ryan, a value in registry terms means the variable name. And the default values in a registry key are the ones without names. I checked for default variables in the function GetValuesAndData using the following code:

    //Display the data of the value. The conditional operatore is
    //needed because the default value has no name
    node.Nodes.Add(value, (value == “” ? “Default” : value) + “: ” + stringData, 2, 2);

    This just displays the word Default when the value has no name. I hope this answered your question.

  3. Iv got my code working after a few hours of tinkering i found that when i set the (default) to “” it doesn’t change it truly to null. if you look in the binary it has some zeros in there when its truly null it says (value not set) in the data field and if you check the binary it will only have 0000 so my question is how would you delete or set the default values data to (value not set ) making it null?

    thank you for your time really appreciate it.
    -Summey

  4. Ryan I think this would do the trick:

    RegistryKey demoKey = Registry.CurrentUser.OpenSubKey(“CodeHill”);
    demoKey.SetValue(“TestValue”, 0, RegistryValueKind.DWord);

  5. Jan

    Thanks for the article.

    If have made a view changes to get to all levels.

    private void UpdateTree()
    {
    TreeNode currentUserNode = new TreeNode(Registry.CurrentUser.Name, 0, 1);
    string[] currentUserSubKeys = Registry.CurrentUser.GetSubKeyNames();

    this.ReadRegistry(currentUserNode, “”, currentUserSubKeys);

    registryTreeView.Nodes.Add(currentUserNode);
    }

    private void ReadRegistry(TreeNode Node, string Key, string[] SubKeys)
    {
    string TotaalPad = Key;
    if (TotaalPad.Length > 0)
    TotaalPad += @”\”;

    foreach (string key in SubKeys)
    {
    //get a list of the next level sub keys and their values
    TreeNode node = new TreeNode(key, 0, 1);
    string[] subKeys = new string[0];
    try
    {
    subKeys = Registry.CurrentUser.OpenSubKey(TotaalPad + key).GetSubKeyNames();
    }
    catch
    {
    }

    if (subKeys.Length > 0)
    {
    this.label1.Text = TotaalPad + key + ” subKeys: ” + subKeys.Length.ToString();
    this.label1.Update();

    this.ReadRegistry(node, TotaalPad + key, subKeys);
    }

    try
    {
    GetValuesAndData(Registry.CurrentUser.OpenSubKey(TotaalPad + key), node);
    }
    catch
    {
    }

    node.Name = node.Text;

    Node.Nodes.Add(node);
    }
    }

  6. Lee Parker

    I’am considering writing my own registry editor, but mostly for remote machines… I will only be accessing HKEY_LOCAL_MACHINE and HKEY_USERS and their sub-keys.

    Can you offer any guidance? This is for a management tool for my companies internal use only – so far I have not been able to figure out how to get regedit.exe to automatically connect to a remote registry with the hostname supplied on the command line. If there is already a freeware product that does this, that would be great, else I’m stuck with writing one.

    Thanks,
    Lee Parker

  7. @Lee Parker, I think you can find tools that can edit a remote PC’s registry in the following links:

    http://www.brothersoft.com/downloads/remote-registry-editor.html
    http://3d2f.com/tags/remote/registry/editor/

    and maybe download.com too. If you decided to develop your own then my advice is to write the code that read and write to the registry in an app that accepts TCP connections and install it in your client PCs. And develop another app that sends the text to it from your PC. This way you dont have to try and manipulate the registry remotely and deal with a lot of networking issues. I hope that helps.

Leave a Reply

Trackback URL | RSS Feed for This Entry