Click here to Skip to main content
Click here to Skip to main content

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.

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

//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))
{
    Console.WriteLine(subKey);
} 

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))
{
   Console.WriteLine(subKey);
}

Get value data

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

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();
SysRegistry.CreateValue(baileySoft.Wmi.Registry.baseKey.HKEY_LOCAL_MACHINE, 
                        registryKey, "SomeValue", "Value01");

Set value data

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

registryKey = @"SOFTWARE\MyWmiApp";
Console.WriteLine("Deleting Value: SomeValue");
SysRegistry.DeleteValue(baileySoft.Wmi.Registry.baseKey.HKEY_LOCAL_MACHINE, 
                        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,
                                            password,
                                            domain,
                                            machine/ip);

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,
                                            null,
                                            null,
                                            machine/ip);

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

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)

Share

About the Author

thund3rstruck
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.

Comments and Discussions

 
QuestionRegistry.RegistryRemote(null, null, null, ""); Pinmembervishal.sharma14128418-Apr-13 17:41 
QuestionMemory Issue PinmemberChad Sikorra9-Apr-13 4:49 
GeneralMy vote of 5 PinmemberMember 917324126-Jun-12 5:06 
QuestionSetDWORDValue PinmemberJakeFront22-Apr-09 23:37 
GeneralRe: SetDWORDValue PinmemberJakeFront23-Apr-09 4:02 
QuestionWIN32_Registry.zip PinmemberThaChief11-Dec-07 2:17 
Hi thund3rstruck,
 
is it possible to get the WIN32_Registry.zip?
That would be realy nice!
 
Thanks in advance
GeneralRemote Registry Pinmembertrevor2x5-Sep-07 12:02 
GeneralRe: Remote Registry Pinmemberthund3rstruck5-Sep-07 13:02 
QuestionThis library is actually slower! PinmemberJcmorin25-Mar-07 2:49 
AnswerRe: This library is actually slower! Pinmemberthund3rstruck25-Mar-07 6:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 19 Oct 2007
Article Copyright 2007 by thund3rstruck
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid