Namespaces and the Base Classes

Accessing the register

This namespace is defined in the same mscorlib assembly that contains the system namespace, so you have access to it without needing to add further references to your project. The remaining registry classes are concerned with security, which is beyond the scope of this chapter.

In order to access a given registry key using the base classes it is necessary to progressively navigate down the registry hierarchy to reach the key. We start off using the Registry class: This class contains static fields that allow access to any of the registry hives. The fields are named:

ClassesRoot
CurrentConfig
CurrentUser
DynData
LocalMachine
PerformanceData
Users

and are all of class RegistryKey. These hives should be familiar to most programmers, though we will comment that PerformanceData may be unfamiliar to some developers. This hive does exist, and is used to access performance counters, but it is not visible in Regedit. The RegistryKey class represents any given key in the registry and has methods to carry out all sorts of operations including accessing and enumerating subkeys, and reading and modifying values. Thus for example, to access the registry key at the top of the ClassesRoot hive, we would use the following:

  RegistryKey hkcr = Registry.ClassesRoot;

and we would then be able to use the variable hkcr to perform operations on that key.

The sample, RegEnumKeys, binds to a registry key, enumerates its subkeys, and displays the name and all values of each subkey. The key we've chosen to bind to is the registry key whose subkeys contain details of all the ADSI providers installed on the computer, HKLM/SOFTWARE/Microsoft/ADs/Providers. When run, the sample gives this output on my machine:

We first ensure that we can access the registry classes without giving full namespace names:

  namespace Wrox.SampleCode.CSharpPreview.ChBaseClasses
  {
  using System;
  using Microsoft.Win32;using System.Drawing;
  using System.Collections;
  using System.ComponentModel;
  using System.WinForms;
  using System.Data;


  using System.IO;

As usual the action takes place in the constructor to the main form class which we've renamed FormRegEnumKeys. We first need to navigate down to the required key. As mentioned earlier, we cannot do this in one go, but have to progressively step down through the registry hierarchy. Note that the name passed to the RegistryKey.OpenSubKey method is not case sensitive:

  public FormRegEnumKeys()
  {
     //
     // Required for Win Form Designer support
     //
     InitializeComponent();

     //
     // TODO: Add any constructor code after InitializeComponent call
     //
    RegistryKey hklm = Registry.LocalMachine;
    RegistryKey software = hklm.OpenSubKey("SOFTWARE");
    RegistryKey microsoft = software.OpenSubKey("Microsoft");
    RegistryKey ads = microsoft.OpenSubKey("ADS");
    RegistryKey prov = ads.OpenSubKey("Providers");

Now we can display the number of subkeys (corresponding in this case to the number of installed ADSI providers), and start to enumerate over each of them in a foreach loop. The following code uses two foreach loops, one to enumerate through the subkeys, and the other to enumerate through each value associated with each subkey.

    AddItem("no. of subkeys is " + prov.SubKeyCount);
    AddItem("");
    AddItem("ADSI Provider registry keys are:");
    string [] sProvNames = prov.GetSubKeyNames();
    foreach (string sName in sProvNames)
    {
       RegistryKey provkey = prov.OpenSubKey(sName);
       AddItem("");
       AddItem(sName + "  (" +
          provkey.ValueCount.ToString() + " values)" );
       foreach (string sValName in provkey.GetValueNames())
       {
          AddItem("   sValName:  " + 
             provkey.GetValue(sValName));
       }
    }


 

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“An expert is a man who has made all the mistakes that can be made in a very narrow field” - Niels Bohr