Getting the name of the installed program, its version and installation date WMI C#

2

I'm trying to get information about the programs installed on remote devices. At the moment, I found a way to output all installed programs. Is there a way to get the version and date of installation?

        List<string> programs = new List<string>();

        ConnectionOptions connectionOptions = new ConnectionOptions();
        connectionOptions.Username = "USERNAME";
        connectionOptions.Password = "USERPASS";
        //connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
        string devicename = textBox8.Text;
        ManagementScope scope = new ManagementScope("\\\\" + devicename + "\\root\\CIMV2", connectionOptions);
        scope.Connect();

        string softwareRegLoc = @"Software\Microsoft\Windows\CurrentVersion\Uninstall";

        ManagementClass registry = new ManagementClass(scope, new ManagementPath("StdRegProv"), null);
        ManagementBaseObject inParams = registry.GetMethodParameters("EnumKey");
        inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE
        inParams["sSubKeyName"] = softwareRegLoc;

        // Read Registry Key Names 
        ManagementBaseObject outParams = registry.InvokeMethod("EnumKey", inParams, null);
        string[] programGuids = outParams["sNames"] as string[];

        foreach (string subKeyName in programGuids)
        {
            inParams = registry.GetMethodParameters("GetStringValue");
            inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE
            inParams["sSubKeyName"] = softwareRegLoc + @"\" + subKeyName;
            inParams["sValueName"] = "DisplayName";
            // Read Registry Value 
            outParams = registry.InvokeMethod("GetStringValue", inParams, null);

            if (outParams.Properties["sValue"].Value != null)
            {
                string softwareName = outParams.Properties["sValue"].Value.ToString();
                programs.Add(softwareName);
            }
        }

        foreach (string softwareName in programs)
        {
            int n = dataGridView8.Rows.Add();
            dataGridView8.Rows[n].DefaultCellStyle.BackColor = Color.LightGreen;
            dataGridView8.Rows[n].Cells[0].Value = devicename;
            dataGridView8.Rows[n].Cells[1].Value = softwareName;
            dataGridView8.FirstDisplayedScrollingRowIndex = dataGridView8.RowCount - 1;
        }
    }
    catch
    {
        MessageBox.Show("Something went wrong");
    } 
}
c#
wmi
asked on Stack Overflow Jul 10, 2018 by Семён • edited Dec 8, 2018 by Cœur

1 Answer

1

Welcome to Stack Overflow. Use ORMi library to get that information easily:

1) Install via NuGet:

Install-Package ORMi

2) Use the library:

using ORMi;

3) Instanciate and use:

WMIHelper helper = new WMIHelper("root\CimV2");

var programs = helper.Query("SELECT Name, Version, InstallDate FROM Win32_Product").ToList();

foreach(var p in programs)
{
    Console.WriteLine(p.Name);
}

And that´s it. Then if you want to do some more advanced working you can define your model classes and query in a simpler way. You can read the docs on GitHub.

answered on Stack Overflow Jul 11, 2018 by NicoRiff

User contributions licensed under CC BY-SA 3.0