how to use WMI to get registry keys

Hi,

I need to access the registry through WMI but i can't find any examples on the internet, can someone help me

tkns


P.S.: the language is C#


Answer this question

how to use WMI to get registry keys

  • eclipse150gt

    the second.

    So in your opinion there are no advantages of using WMI in this situation (by the way, i have admin privileges on the remote machine)

  • Cygon4

    sorry, which link is this for the first or second

    I think you could still use the .NET way of accessing a remote registry key, again, security permissions will be an issue. I'll see what I can dig up

    http://msdn2.microsoft.com/en-us/library/microsoft.win32.registrykey.openremotebasekey.aspx

     



  • DBFrustrater

    it depends on permission really, and authentication. If you give it the correct credentials either using the .NET Classes, or WMI, then it will work AFAIK. its better to go through the .NET Route as its cheaper and faster than invoking WMI.

  • ananda vardhana

    well, one thing i didn't mentioned was that the registry i want to access is on a remote machine, that's why i want to use WMI.

  • j1m68

    why dont you use the Microsoft.Win32 namespace to access the registry Easier and faster than WMI Either way you may still have security issues.

    http://msdn2.microsoft.com/en-us/library/microsoft.win32.registry.aspx

     

    http://msdn2.microsoft.com/en-us/library/aa394600.aspx - even though this is in VBScript, easily convertable, hopefully, to C#



  • tonn

    I've been trying to understand the VB code but i don't know how can i execute methods from providers using C# and the fact that the StdRegProv Requires Stdprov.dll is also a problem because i don't know where to find it. :\

    Can you show me some sort of example in C#

  • Alix


    //const uint CLASSES_ROOT = 0x80000000;
    //const uint CURRENT_USER = 0x80000001;
    //const uint USERS = 0x80000003;
    //const uint CURRENT_CONFIG = 0x80000005;
    const uint LOCAL_MACHINE = 0x80000002;

    ConnectionOptions options = new ConnectionOptions();
    options.Impersonation = ImpersonationLevel.Impersonate;
    options.EnablePrivileges = true;

    options.Password = password; //null if u want to access the local machine
    options.Username = user; //null if u want to access the local machine


    ManagementScope myScope = new ManagementScope("\\\\" + host + "\\root\\default", options);
    ManagementPath mypath = new ManagementPath("StdRegProv");
    ManagementClass mc = new ManagementClass(myScope, mypath, null);

    ManagementBaseObject inParams = mc.GetMethodParameters("GetStringValue");
    inParams["hDefKey"] = LOCAL_MACHINE;
    inParams["sSubKeyName"] = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion";
    inParams["sValueName"] = "ProgramFilesDir";

    ManagementBaseObject outParams = mc.InvokeMethod("GetStringValue", inParams, null);

    if (Convert.ToUInt32(outParams["ReturnValue"]) == 0)
    {
    programFilesPath = outParams["sValue"].ToString();
    }
    else
    {
    programFilesPath = "N/A";
    }

    Console.WriteLine("%ProgramFiles% - " + programFilesPath);

  • how to use WMI to get registry keys