Thursday, 7 January 2010

Friendly OS Name from WMI

When you want to find out what operating system your application is running on, System.Environment.OSVersion is not very readable.  You can get a nice friendly name using WMI like this:

  1: private static string GetOSName()
  2: {
  3:    const string query = "SELECT * FROM Win32_OperatingSystem";
  4:    var searcher = new ManagementObjectSearcher(query);
  5:    var results = from x in searcher.Get().OfType<ManagementObject>()
  6:            select x.GetPropertyValue("Caption");
  7:    return results.Any() ? results.First().ToString() : "Unknown";
  8: }


This will give you something like "Microsoft® Windows Server® 2008 Enterprise ".

1 comment:

Anonymous said...

good tip....