Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 7th, 2013  |  syntax: None  |  size: 3.86 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. select *  from Win32_OperatingSystem
  2.        
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Management;
  6. using System.Text;
  7.  
  8. namespace GetWMI_Info
  9. {
  10.     class Program
  11.     {
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             try
  16.             {
  17.                 List<string> sLookUp = new List<string>();
  18.                 ManagementClass manClass = new ManagementClass("Win32_OperatingSystem");
  19.                 manClass.Options.UseAmendedQualifiers = true;
  20.                 foreach (PropertyData Property in manClass.Properties)
  21.                     if (Property.Name.Equals("OperatingSystemSKU"))                    
  22.                         foreach (QualifierData Qualifier in Property.Qualifiers)
  23.                             if (Qualifier.Name.Equals("Values"))
  24.                                 foreach (String s in (System.String[])Qualifier.Value)
  25.                             sLookUp.Add(s);                  
  26.  
  27.  
  28.                 ManagementScope Scope;                
  29.                 Scope = new ManagementScope(String.Format("\\{0}\root\CIMV2", "."), null);
  30.                 Scope.Connect();
  31.                 ObjectQuery Query = new ObjectQuery("SELECT OperatingSystemSKU FROM Win32_OperatingSystem");
  32.                 ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
  33.  
  34.                 foreach (ManagementObject WmiObject in Searcher.Get())
  35.                 {
  36.                     Console.WriteLine("{0} {1}", "OperatingSystemSKU", sLookUp[Convert.ToInt32((UInt32)WmiObject["OperatingSystemSKU"])]);// Uint32
  37.                 }
  38.             }
  39.             catch (Exception e)
  40.             {
  41.                 Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
  42.             }
  43.             Console.WriteLine("Press Enter to exit");
  44.             Console.Read();
  45.         }
  46.     }
  47. }
  48.        
  49. string OSname = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>() select x.GetPropertyValue("Caption")).First().ToString();
  50.        
  51. MessageBox.Show((from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>() select x.GetPropertyValue("Caption")).First().ToString());
  52.        
  53. /// <summary>
  54. /// Class for getting environment information
  55. /// </summary>
  56. public static class EnvironmentInfo
  57. {
  58.     /// <summary>
  59.     /// Gets environment information by querying the system
  60.     /// </summary>
  61.     public static IEnumerable<string> GetEnvironmentInfo()
  62.     {
  63.         List<string> results = new List<string>();
  64.  
  65.         SafeUpdateListOfResultsFromInstrumentation("OS Product: {0}", results, "select * from win32_OperatingSystem", "name");
  66.         SafeUpdateListofResults("OS Version: {0}", results, (() => Environment.OSVersion.ToString()));
  67.  
  68.         return results;
  69.     }
  70.  
  71.     private static void SafeUpdateListofResults(string format, List<string> results, Func<string> del)
  72.     {
  73.         try
  74.         {
  75.             string str = del.Invoke();
  76.             results.Add(string.Format(format, str));
  77.         }
  78.         catch (Exception)
  79.         {
  80.             //Swallow exception - can't get diagnostic info!
  81.         }
  82.     }
  83.  
  84.     private static void SafeUpdateListOfResultsFromInstrumentation(string format, List<string> results, string query, string index)
  85.     {
  86.         try
  87.         {
  88.             WqlObjectQuery objectQuery = new WqlObjectQuery(query);
  89.             ManagementObjectSearcher searcher = new ManagementObjectSearcher(objectQuery);
  90.  
  91.             string name, value;
  92.  
  93.             foreach (ManagementObject managementObject in searcher.Get())
  94.             {
  95.                 name = managementObject[index].ToString();
  96.                 string[] split1 = name.Split('|');
  97.                 value = split1[0];
  98.                 results.Add(string.Format(format, value));
  99.             }
  100.         }
  101.         catch (Exception)
  102.         {
  103.             //Swallow exception - can't get diagnostic info!
  104.         }
  105.  
  106.     }
  107. }