using System; using System.Management; namespace IsConnected { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { try { // build the base query string, requesting all // records for the specified configuration class String query = "select * from MSNdis_MediaConnectStatus"; // The ManagementObjectSearcher class is used to build out a collection // of WMI instances using a specified query and namespace // ConfigNamespace == "root\\WMI" ManagementObjectSearcher searcher = new ManagementObjectSearcher( "root/WMI", query); // display the query we're attempting to execute Console.WriteLine("\n"+ query + "\n"); // class to hold collection of WMI instances retrieved by querying // a ManagementObjectSearcher instance. Each invocation of // Get() re-executes the query and returns a new collection ManagementObjectCollection collection = searcher.Get(); // int to count objects in the collection int count = 0; foreach (ManagementObject item in collection) { foreach (PropertyData property in item.Properties) { // since /nometa was not specified // write the propert name Console.Write(property.Name + ": "); // write the property value Console.WriteLine(property.Value); } count++; } if (count == 0) { // an empty collection was retrieved Console.WriteLine("No information found" ); } } // handler for ManagementExceptions catch (ManagementException e) { Console.WriteLine("\nError : " + e.Message); // ErrorInformation is the extended error // object provided by WMI if (null != e.ErrorInformation) { // print out extended error information Console.WriteLine("Description : " + e.ErrorInformation["Description"]); Console.WriteLine("Operation : " + e.ErrorInformation["Operation"]); Console.WriteLine("ParameterInfo: " + e.ErrorInformation["ParameterInfo"]); Console.WriteLine("Provider : " + e.ErrorInformation["ProviderName"]); Console.WriteLine("StatusCode : {0:X}", e.ErrorInformation["StatusCode"]); } } // general handler for all other exceptions catch (Exception ex) { Console.WriteLine("Error : " + ex.Message); } } } }