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:
- Local Registry
- Remote Registry
Local system Registry
This code deals with tasks performed on the physical machine it is running on.
Instantiate the local client
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");
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.
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!