Win32_Product InstallLocation (error)

Posted by andrew on Stack Overflow See other posts from Stack Overflow or by andrew
Published on 2010-06-02T16:39:30Z Indexed on 2010/06/02 16:44 UTC
Read the original article Hit count: 157

Filed under:
|

in C#, i'm trying to get some properties from the instances of Win32_Product, but i seem to have an error saying "Object reference not set to an instance of an object."

here's the code:

class Package {
 public string productName;
 public string installDate;
 public string installLocation;
}

class InstalledPackages
{
    public static List<Package> get()
    {
        List<Package> packages = new List<Package>();
        string query = "SELECT * FROM Win32_Product";
        ManagementScope oMs = new ManagementScope();
        ObjectQuery oQuery = new ObjectQuery(query);
        ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
        ManagementObjectCollection installedPackages = oSearcher.Get();
        foreach (ManagementObject package in installedPackages)
        {
            Package p = new Package();
            p.productName = package["Name"].ToString();
            p.installLocation = package["InstallLocation"].ToString();
            p.installDate = package["InstallDate"].ToString();
            packages.Add(p);
        }
        return packages;
    }
}

the exception appears when it gets to

p.installLocation = package["InstallLocation"].ToString();

also, i get one if i try to do

p.installLocation = package["InstallDate2"].ToString();

if i'm asking for InstallDate it works.

(i'm using Windows 7 Ultimate x64)

© Stack Overflow or respective owner

Related posts about c#

Related posts about wmi