Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Book Cover C# Programming with the Public Beta
40034 times
Rated
Read 40,034 times

Contents

Related Categories

Namespaces and the Base Classes - Accessing the register

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


 

Comments

  • Re: [1676] Namespaces and the Base Classes

    Posted by cdm on 06 Jun 2008

    Are you kidding? System.IO.File and System.IO.Directory are static classes. They can't be instantiated. Did any of the authors or editors bother running this code to see if it would work?

  • MSMQ TriggerEvents C#

    Posted by amaretto on 04 Jun 2003

    I'm struggeling with the implementation of simple asynchroneous receive in C# :
    aQueue.enableNotification(eventObject, ref cursor, ref timeout)

    cursor and timeout are values (int) that can be retr...

  • Mistakes?

    Posted by mitzvah on 13 Nov 2002

    After spend a whole afternoon testing code in this article( they didn't work), I found this in msdn:
    [b]All methods of the Directory class are static and can therefore be called without having an ins...