Click here to Skip to main content
15,896,207 members
Articles / Programming Languages / C#

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 106.1K   2.9K   75  
A class library that wraps WMI Win32 Registry.
using System;
using System.Collections.Generic;
using System.Text;

namespace baileySoft.Wmi
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate Remote Client
            //This is commented out since you must specify your details in order for it to work.
            //Registry.RegistryObject SysRegistry =
            //    new Registry.RegistryRemote("neal.bailey",
            //                                "S3cr3tPa$$",
            //                                "BAILEYSOFT",
            //                                "192.168.2.1");

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

            //Enumerate Values
            registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
            Console.WriteLine("");
            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";
            Console.WriteLine("");
            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("");
            Console.WriteLine("Creating Key: " + registryKey);
            SysRegistry.CreateKey(baileySoft.Wmi.Registry.baseKey.HKEY_LOCAL_MACHINE, registryKey);

            //Create Value
            registryKey = @"SOFTWARE\MyWmiApp";
            Console.WriteLine("");
            Console.WriteLine("Creating Value: SomeValue");
            SysRegistry.CreateValue(baileySoft.Wmi.Registry.baseKey.HKEY_LOCAL_MACHINE, 
                                                        registryKey, "SomeValue", "Value01");


            //Set Value
            registryKey = @"SOFTWARE\MyWmiApp";
            Console.WriteLine("");
            Console.WriteLine("Setting SomeValue: Value02");
            SysRegistry.SetValue(baileySoft.Wmi.Registry.baseKey.HKEY_LOCAL_MACHINE, 
                  registryKey, "SomeValue", "Value02", baileySoft.Wmi.Registry.valueType.STRING);

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

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

            Console.WriteLine("");
            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);
            Console.ReadLine();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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