HOME  |    TRAINING  |   FREE TUTORIALS   |   JOBS
Find out more about our new RSS feed.
FREE Tutorial
C# PROGRAMMING: NAMESPACES AND THE BASE CLASSES PART 6 - ACCESSING THE REGISTER

CATEGORY
SEARCH OUR OTHER TUTORIALS

DESCRIPTION

In this section we'll present a code sample that enumerates registry keys and reads their values. There are several classes used to access the registry, of which we will use two: Registry, and RegistryKey, both of which are in the Microsoft.Win32 namespace.
Click here to be kept informed of our new Tutorials.


This free tutorial is a sample from the book C# Programming with the Public Beta.


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




5 RELATED COURSES AVAILABLE
C# PROGRAMMING
At the end of this course, you will have learned the fundamental skills that are required to design and develop o....
C++ PROGRAMMING
Object oriented programming is fast becoming the leading software design methodology, with C++ becoming ever more....
C PROGRAMMING
This course is design to provide non-C programmers with the essential skills and knowledge necessary to allow the....
C PROGRAMMING IN THE UNIX ENVIRONMENT
This course will provide readers the knowledge to use many of the UNIX 'C' library facilities, interface with the....
MICROSOFT VISUAL C++ 5 AND THE MFC&T 32 BIT WINDOWS PROGRAMMING
A complete 32 bit programming course highlighting the main features regarding the Microsoft Visual C++ programmin....
 
0 RELATED JOBS AVAILABLE
CONTACT US
Sunday 7th September 2008  © COPYRIGHT 2008 - VISUALSOFT