Click here to Skip to main content
15,878,959 members
Articles / Programming Languages / C#
Article

Howto: (Almost) Everything in WMI via C# - Part 1: Registry

Rate me:
Please Sign up or sign in to vote.
3.71/5 (14 votes)
19 Oct 2007CPOL2 min read 105.8K   2.9K   75   13
A class library that wraps WMI Win32 Registry.

Introduction

The attached class library is the first of a series of libraries I will be posting in which I share the fruit of my labors in converting my VBScript/VB.NET WMI code into C# .NET code using the System.Management namespaces in .NET. This first library focuses on the Win32 Registry. I know there are already some fantastic classes out there in the Microsoft.Win32 namespace for working with the Registry. This article is geared towards those of us out there that have a hard time letting go of WMI. We know how it works and are comfortable with it.

Basically, this library consists of five classes:

  • baileysoft.Wmi.Registry.RegistryConnection
  • baileysoft.Wmi.Registry.RegistryLocal
  • baileysoft.Wmi.Registry.RegistryMethod
  • baileysoft.Wmi.Registry.RegistryObject
  • baileysoft.Wmi.Registry.RegistryRemote

The abstract base class RegistryObject contains all the WMI object properties and abstract method signatures. The RegistryRemote and RegistryLocal classes inherit RegistryObject. The RegistryConnection class facilitates creating the WMI connection, the RegistryMethod class facilitates executing the WMI methods, and the enum file contains the enumerations.

Below, you will find sample code for executing all the procedures available.

Using the attached code

The project attached clearly outlines everything you can do with the classes. You just need to open it up in VS2005, and you're ready to go. This article is partitioned in to two:

  1. Local Registry
  2. Remote Registry

Local system Registry

This code deals with tasks performed on the physical machine it is running on.

Instantiate the local client

C#
//Your Class is using namespace baileySoft.Wmi (or attached to)
Registry.RegistryObject SysRegistry = new Registry.RegistryLocal();

Enumerate keys

C#
string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion";
Registry.RegistryObject SysRegistry = new Registry.RegistryLocal();
Console.WriteLine("SubKeys Under: " + registryKey);
            
foreach (string subKey in SysRegistry.EnumerateKeys(
         baileySoft.Wmi.Registry.baseKey.HKEY_LOCAL_MACHINE, registryKey))
{
    Console.WriteLine(subKey);
} 

Enumerate values in a key

C#
registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
Registry.RegistryObject SysRegistry = new Registry.RegistryLocal();
Console.WriteLine("Values Under: " + registryKey);
     
foreach (string subKey in SysRegistry.EnumerateValues(
        baileySoft.Wmi.Registry.baseKey.HKEY_LOCAL_MACHINE, registryKey))
{
   Console.WriteLine(subKey);
}

Get value data

C#
registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
Registry.RegistryObject SysRegistry = new Registry.RegistryLocal();
Console.WriteLine("Getting Value Data For Value Daemon Tools");
Console.WriteLine(SysRegistry.GetValue(baileySoft.Wmi.Registry.baseKey.HKEY_LOCAL_MACHINE,
                                       registryKey, "DAEMON Tools",
                                       baileySoft.Wmi.Registry.valueType.STRING));

Create key

C#
registryKey = @"SOFTWARE\MyWmiApp";
Console.WriteLine("Creating Key: " + registryKey);
Registry.RegistryObject SysRegistry = new Registry.RegistryLocal();
SysRegistry.CreateKey(baileySoft.Wmi.Registry.baseKey.HKEY_LOCAL_MACHINE, registryKey);

Create value

C#
registryKey = @"SOFTWARE\MyWmiApp";
Console.WriteLine("Creating Value: SomeValue");
Registry.RegistryObject SysRegistry = new Registry.RegistryLocal();
SysRegistry.CreateValue(baileySoft.Wmi.Registry.baseKey.HKEY_LOCAL_MACHINE, 
                        registryKey, "SomeValue", "Value01");

Set value data

C#
registryKey = @"SOFTWARE\MyWmiApp";
Console.WriteLine("Setting SomeValue: Value02");
Registry.RegistryObject SysRegistry = new Registry.RegistryLocal();
SysRegistry.SetValue(baileySoft.Wmi.Registry.baseKey.HKEY_LOCAL_MACHINE, 
                     registryKey, "SomeValue", "Value02", 
                     baileySoft.Wmi.Registry.valueType.STRING);

Delete a value

C#
registryKey = @"SOFTWARE\MyWmiApp";
Console.WriteLine("Deleting Value: SomeValue");
SysRegistry.DeleteValue(baileySoft.Wmi.Registry.baseKey.HKEY_LOCAL_MACHINE, 
                        registryKey, "Value01");

Delete a Key

C#
registryKey = @"SOFTWARE\MyWmiApp";
Console.WriteLine("Deleting Key: " + registryKey);
Registry.RegistryObject SysRegistry = new Registry.RegistryLocal();
SysRegistry.DeleteKey(baileySoft.Wmi.Registry.baseKey.HKEY_LOCAL_MACHINE, registryKey);

Enumerate Win32 properties

C#
Registry.RegistryObject SysRegistry = new Registry.RegistryLocal();
Console.WriteLine("Getting System Registry Settings");
Console.WriteLine("Caption: " + SysRegistry.Caption);
Console.WriteLine("Current Size: " + SysRegistry.CurrentSize);
Console.WriteLine("Description: " + SysRegistry.Description);
Console.WriteLine("Install Date: " + SysRegistry.InstallDate);
Console.WriteLine("Max Size: " + SysRegistry.MaximumSize);
Console.WriteLine("Name: " + SysRegistry.Name);
Console.WriteLine("Proposed Size: " + SysRegistry.ProposedSize);
Console.WriteLine("Status: " + SysRegistry.Status);

Remote system Registry

In order to run the code above against a remote machine, you must instantiate the RegistryRemote class. During this instantiation, you need to either send in explicit credentials, or you can send in null values if you're running this on a workstation on a domain, logged in with a domain account with the appropriate permissions to perform these tasks against the remote workstation.

Instantiating the remote Registry class

C#
Registry.RegistryObject SysRegistry =
                new Registry.RegistryRemote(userName,
                                            password,
                                            domain,
                                            machine/ip);

Connecting to a remote machine where you want to use the domain credentials from the logged-in user

C#
Registry.RegistryObject SysRegistry =
                new Registry.RegistryRemote(null,
                                            null,
                                            null,
                                            machine/ip);

An example of a remote Registry task (Using a service account)

C#
Registry.RegistryObject SysRegistry =
                new Registry.RegistryRemote("neal.bailey",
                                            "S3cr3tPa$$",
                                            "BAILEYSOFT",
                                            "192.168.2.1");
//Enumerate Keys on Remote Registry

string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion";
Console.WriteLine("SubKeys Under: " + registryKey);
            
foreach (string subKey in SysRegistry.EnumerateKeys(
              baileySoft.Wmi.Registry.baseKey.HKEY_LOCAL_MACHINE, registryKey))
{
   Console.WriteLine(subKey);
}

Conclusion

The WMI (Windows Management Instrumentation) provider is considerably slower than the native .NET classes. At first, it may seem pointless to use WMI for Registry management tasks, considering the ease of use of the .NET Registry classes; however, a lot of developers out there spent a lot of time learning WMI, and would like to have it available in their toolbox.

History

  • Originally submitted on 24 March 2007.

License

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


Written By
Software Developer
United States United States
I'm a professional .NET software developer and proud military veteran. I've been in the software business for 20+ years now and if there's one lesson I have learned over the years, its that in this industry you have to be prepared to be humbled from time to time and never stop learning!

Comments and Discussions

 
Questionset default value Pin
Member 1574507027-Aug-22 17:50
Member 1574507027-Aug-22 17:50 
QuestionRemote connection Pin
Member 1125227026-May-17 6:37
Member 1125227026-May-17 6:37 
QuestionRegistry.RegistryRemote(null, null, null, ""); Pin
vishal.sharma14128418-Apr-13 17:41
vishal.sharma14128418-Apr-13 17:41 
QuestionMemory Issue Pin
Chad Sikorra9-Apr-13 4:49
Chad Sikorra9-Apr-13 4:49 
GeneralMy vote of 5 Pin
Member 917324126-Jun-12 5:06
Member 917324126-Jun-12 5:06 
QuestionSetDWORDValue Pin
JakeFront22-Apr-09 23:37
professionalJakeFront22-Apr-09 23:37 
GeneralRe: SetDWORDValue Pin
JakeFront23-Apr-09 4:02
professionalJakeFront23-Apr-09 4:02 
AnswerRe: SetDWORDValue Pin
doug z5-Dec-14 12:06
doug z5-Dec-14 12:06 
QuestionWIN32_Registry.zip Pin
ThaChief11-Dec-07 2:17
ThaChief11-Dec-07 2:17 
GeneralRemote Registry Pin
trevor2x5-Sep-07 12:02
trevor2x5-Sep-07 12:02 
GeneralRe: Remote Registry Pin
thund3rstruck5-Sep-07 13:02
thund3rstruck5-Sep-07 13:02 
QuestionThis library is actually slower! Pin
Jcmorin25-Mar-07 2:49
Jcmorin25-Mar-07 2:49 
AnswerRe: This library is actually slower! Pin
thund3rstruck25-Mar-07 6:03
thund3rstruck25-Mar-07 6:03 

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.