CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2004
    Posts
    1,361

    Using WMI in C# to Enumerate Registry Values and Keys

    I have been beating my head against the wall trying out how to do this. I know it is a simply syntactical problem. I do not know how to form the query string.

    I have been digging and digging and all I can find VBS scripts that do this and I have no idea how to do the equivalent in C#. I do know how to do some things, like look at processes or services. They do not need additional parameters, you just call the function and look at the responses.

    IE:
    Code:
    //Make a connection to a remote computer using these options
    		ManagementScope scope = new ManagementScope("\\\\198.31.1.1\\root\\cimv2", options);
    		scope.Connect();
    
    
    		// Build a query for enumeration of Win32_Environment instances
    		SelectQuery query = new SelectQuery("Win32_Process");
    
    		// Instantiate an object searcher with this query
    		ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); 
    
    		// Call Get() to retrieve the collection of objects and loop through it
    		foreach (ManagementBaseObject envVar in searcher.Get())
    			Console.WriteLine("Variable : {0}, Value = {1}", 
    				envVar["Name"],envVar["ExecutablePath"]);

    Now how do you turn that into one that can enumerate all keys under HKLM / SOFTWARE?

    The only part I know I have right, or at least I think I do is :
    Code:
    ManagementScope scope = new ManagementScope("\\\\198.31.1.1\\root\\default:StdRegProv", options);
    What I don't know what to is how to change this:
    Code:
    SelectQuery query = new SelectQuery("Win32_Process");

    Secondly:
    How are you supposed to find out what everything looks like so you can make queries for any objects and then know what 'values' to look for? I can't find any list of all the 'objects' I query, let alone adding things like all the values and whatnot assocated with them.

    At best I find hoards of VBS scripts using WMI and I have no how to translate them into a C# query call.

    For example:
    Code:
    HKLM = 2147483650 ;HKEY_LOCAL_MACHINE
    sRegPath = "SYSTEM\CurrentControlSet\Services\Eventlog\System";
    
    oLoc = CreateObject("WbemScripting.SWbemLocator")
    oSvc = oLoc.ConnectServer(computername, "root/default",user,password)
    oReg = oSvc.Get("StdRegProv")
    oMethod = oReg.Methods_.Item("EnumKey");
    oInParam = oMethod.InParameters.SpawnInstance_();
    oInParam.hDefKey = HKLM;
    oInParam.sSubKeyName = sRegPath;
    oOutParam = oReg.ExecMethod_(oMethod.Name, oInParam);    
    aNames = oOutParam.sNames;
    
    subkeylist = ""
    
    for i = 0 to ArrInfo(aNames,6)-1
    	 name = aNames[i]
        ;Message("Subkeys", StrCat("KeyName: ", name));
        subkeylist = StrCat(subkeylist,@tab,name)
    Next
    subkeylist = StrTrim(subkeylist)
    AskItemList(sRegPath,subkeylist,@tab,@unsorted,@single)
    Is a VBS script that uses WMI to enum SYSTEM\CurrentControlSet\Services\Eventlog\System registry.

    Which is pretty much what I want to do, but again, I have no idea how to do that in C# using WMI.

  2. #2
    Join Date
    Sep 2004
    Posts
    1,361

    Re: Using WMI in C# to Enumerate Registry Values and Keys

    Nobody has any ideas on this? I posted this on the MS dotnet forums, and didn't get any answers either. This can't be that obscure, can it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured