Click here to Skip to main content
13,253,507 members (65,725 online)
Click here to Skip to main content
Add your own
alternative version


72 bookmarked
Posted 23 Mar 2007

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

, 19 Oct 2007
Rate this:
Please Sign up or sign in to vote.
A class library that wraps WMI Win32 Registry.


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

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

Enumerate keys

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

Enumerate values in a key

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

Get value data

registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
Registry.RegistryObject SysRegistry = new Registry.RegistryLocal();
Console.WriteLine("Getting Value Data For Value Daemon Tools");
                                       registryKey, "DAEMON Tools",

Create key

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

registryKey = @"SOFTWARE\MyWmiApp";
Console.WriteLine("Creating Value: SomeValue");
Registry.RegistryObject SysRegistry = new Registry.RegistryLocal();
                        registryKey, "SomeValue", "Value01");

Set value data

registryKey = @"SOFTWARE\MyWmiApp";
Console.WriteLine("Setting SomeValue: Value02");
Registry.RegistryObject SysRegistry = new Registry.RegistryLocal();
                     registryKey, "SomeValue", "Value02", 

Delete a value

registryKey = @"SOFTWARE\MyWmiApp";
Console.WriteLine("Deleting Value: SomeValue");
                        registryKey, "Value01");

Delete a Key

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

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

Registry.RegistryObject SysRegistry =
                new Registry.RegistryRemote(userName,

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

Registry.RegistryObject SysRegistry =
                new Registry.RegistryRemote(null,

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

Registry.RegistryObject SysRegistry =
                new Registry.RegistryRemote("neal.bailey",
//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))


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.


  • Originally submitted on 24 March 2007.


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


About the Author

Software Developer
United States United States
I'm a typical 30 year old generation X guy that likes video games, NFL football, and comic style art. I have an insatiable passion for programming and doing what ever it takes to become a better programmer.

You may also be interested in...


Comments and Discussions

QuestionRemote connection Pin
Member 1125227026-May-17 7:37
memberMember 1125227026-May-17 7:37 
QuestionRegistry.RegistryRemote(null, null, null, ""); Pin
vishal.sharma14128418-Apr-13 18:41
membervishal.sharma14128418-Apr-13 18:41 
QuestionMemory Issue Pin
Chad Sikorra9-Apr-13 5:49
memberChad Sikorra9-Apr-13 5:49 
GeneralMy vote of 5 Pin
Member 917324126-Jun-12 6:06
memberMember 917324126-Jun-12 6:06 
Great resource. Well done
QuestionSetDWORDValue Pin
JakeFront23-Apr-09 0:37
memberJakeFront23-Apr-09 0:37 
GeneralRe: SetDWORDValue Pin
JakeFront23-Apr-09 5:02
memberJakeFront23-Apr-09 5:02 
AnswerRe: SetDWORDValue Pin
doug z5-Dec-14 13:06
memberdoug z5-Dec-14 13:06 Pin
ThaChief11-Dec-07 3:17
memberThaChief11-Dec-07 3:17 
GeneralRemote Registry Pin
trevor2x5-Sep-07 13:02
membertrevor2x5-Sep-07 13:02 
GeneralRe: Remote Registry Pin
thund3rstruck5-Sep-07 14:02
memberthund3rstruck5-Sep-07 14:02 
QuestionThis library is actually slower! Pin
Jcmorin25-Mar-07 3:49
memberJcmorin25-Mar-07 3:49 
AnswerRe: This library is actually slower! Pin
thund3rstruck25-Mar-07 7:03
memberthund3rstruck25-Mar-07 7: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 19 Oct 2007
Article Copyright 2007 by thund3rstruck
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid