public void ClassMembers_For_Win32_LogicalDisk() { var managementClass = new ManagementClass(new ManagementPath("Win32_LogicalDisk")); MethodDataCollection methods = managementClass.Methods; Assert.True(methods.Count > 0); foreach (MethodData method in methods) { Assert.False(string.IsNullOrWhiteSpace(method.Name)); } PropertyDataCollection properties = managementClass.Properties; Assert.True(properties.Count > 0); foreach (PropertyData property in properties) { Assert.False(string.IsNullOrWhiteSpace(property.Name)); } QualifierDataCollection qualifiers = managementClass.Qualifiers; foreach (QualifierData qualifier in qualifiers) { Assert.False(string.IsNullOrWhiteSpace(qualifier.Name)); } }
public static void Main() { // Get the WMI class ManagementClass mClass = new ManagementClass("Win32_Service"); mClass.Options.UseAmendedQualifiers = true; // Get the Qualifiers for the class QualifierDataCollection qualifiers = mClass.Qualifiers; // display the Qualifier names Console.WriteLine(mClass.ClassPath.ClassName + " Qualifiers: "); foreach (QualifierData q in qualifiers) { Console.WriteLine(q.Name); } Console.WriteLine(); Console.WriteLine("Class Description: "); Console.WriteLine( mClass.Qualifiers["Description"].Value); }
public static void ManagementDemo1() { string moAddress = " "; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc2 = mc.GetInstances(); foreach (ManagementObject mo in moc2) { ManagementPath mp = mo.ClassPath; IContainer ic = mo.Container; ObjectGetOptions ogo = mo.Options; ManagementPath mpa = mo.Path; PropertyDataCollection pdc = mo.Properties; foreach (PropertyData item in pdc) { Console.WriteLine($"{item.Name} ={mo[item.Name]}"); } Console.WriteLine("************************************************************"); PropertyDataCollection.PropertyDataEnumerator pde = pdc.GetEnumerator(); QualifierDataCollection qd = mo.Qualifiers; ManagementScope ms = mo.Scope; ISite isi = mo.Site; PropertyDataCollection pdct = mo.SystemProperties; if ((bool)mo["IPEnabled"] == true) { moAddress = mo["MacAddress"].ToString(); } mo.Dispose(); } }
}//public static void Query() ///<summary>Query</summary> ///<param name="WMIPath">WMIPath</param> ///<param name="exceptionMessage">exceptionMessage</param> public static void Query ( ref String WMIPath, ref String exceptionMessage ) { ManagementClass managementClass = null; ManagementObjectCollection managementObjectCollection = null; ManagementObjectSearcher managementObjectSearcher = null; QualifierDataCollection qualifierDataCollection = null; PropertyDataCollection propertyDataCollection = null; SelectQuery selectQuery = null; try { managementClass = new ManagementClass( WMIPath ); managementClass.Options.UseAmendedQualifiers = true; qualifierDataCollection = managementClass.Qualifiers; foreach ( QualifierData qualifierData in qualifierDataCollection ) { System.Console.WriteLine ( qualifierData.Name + " = " + qualifierData.Value ); }//foreach ( QualifierData qualifierData in qualifierDataCollection ) //Build a query for enumeration of instances selectQuery = new SelectQuery( WMIPath ); //Instantiate an object searcher with this query managementObjectSearcher = new ManagementObjectSearcher( selectQuery ); //Call Get() to retrieve the collection of objects and loop through it managementObjectCollection = managementObjectSearcher.Get(); foreach (ManagementBaseObject managementBaseObject in managementObjectCollection ) { propertyDataCollection = managementBaseObject.Properties; foreach ( PropertyData propertyData in propertyDataCollection ) { System.Console.WriteLine ( "{0}: {1}", propertyData.Name, managementBaseObject[ propertyData.Name ] ); }//foreach ( PropertyData propertyData in propertyDataCollection ) }//foreach (ManagementBaseObject managementBaseObject in managementObjectCollection ) }//try catch ( Exception exception ) { exceptionMessage = "Exception: " + exception.Message; System.Console.WriteLine( "Exception: {0}", exception.Message ); }//catch ( Exception exception ) }//public static void Query()
static string ToMofString(QualifierDataCollection qualifiers, string prefix, string suffix) { var qualifierString = string.Empty; var qualifierList = new List<string>(); var maxQualifierLength = 0; foreach (QualifierData qualifier in qualifiers) { if (qualifier.Name == "CIMTYPE") continue; var strQualifier = qualifier.Name; if (qualifier.Value.GetType() == typeof(String)) strQualifier += $"(\"{qualifier.Value}\")"; else if (qualifier.Value.GetType() == typeof(Int32)) strQualifier += $"({qualifier.Value})"; else if (qualifier.Value.GetType() == typeof(Boolean)) { } // add nothing else if (qualifier.Value.GetType().IsArray) { var qualifierValueList = new List<string>(); foreach (var element in (Array)qualifier.Value) qualifierValueList.Add(element.GetType() == typeof(String) ? $"\"{element}\"" : $"{element}"); strQualifier += $"{{{string.Join(", ", qualifierValueList)}}}"; } else throw new ApplicationException($"Unsupported Qualifier Type - {qualifier.Value.GetType().Name}"); if (qualifier.PropagatesToInstance) strQualifier += ": ToInstance"; maxQualifierLength = strQualifier.Length > maxQualifierLength ? strQualifier.Length : maxQualifierLength; qualifierList.Add(strQualifier); } var orderedQualifierList = new List<string>(); foreach (var nextQualifierInOrder in qualifierOrder) { var foundQualifier = string.Empty; foreach (var qualifier in qualifierList) { if (qualifier.ToLower().StartsWith(nextQualifierInOrder)) { orderedQualifierList.Add(qualifier); continue; } } } if (qualifierList.Count != orderedQualifierList.Count) throw new ApplicationException($"Unknown Qualifier Type(s) - {String.Join(",", qualifierList.ToArray())}"); if (orderedQualifierList.Count > 0) { var wrapQualifiers = maxQualifierLength > 80; var header = "["; var delimiter = wrapQualifiers ? $",\n{prefix} " : ", "; var footer = wrapQualifiers ? $"\n{prefix}]{suffix}" : $"]{suffix}"; qualifierString = $"{header}{string.Join(delimiter, orderedQualifierList)}{footer}"; } return qualifierString; }
static string Serialize(QualifierDataCollection properties) { NativeCimQualifiers obj = new NativeCimQualifiers(); foreach (var q in properties) { obj.Add(q.Name, q.IsAmended, q.IsLocal, q.IsOverridable, q.PropagatesToInstance, q.PropagatesToSubclass, q.Value); } return(NativeCimQualifiersHelper.Serialize(obj)); }
public static void ToDebug(this QualifierDataCollection collection) { if (collection == null || collection.Count <= 0) { return; } Console.WriteLine("Qualifiers:"); foreach (QualifierData data in collection) { ToDebug(data); } }
public static void Main() { // Get the WMI class ManagementPath p = new ManagementPath("Win32_Process"); // Options specify that amended qualifiers // are to be retrieved along with the class ObjectGetOptions o = new ObjectGetOptions( null, System.TimeSpan.MaxValue, true); ManagementClass c = new ManagementClass(p, o); // Get the methods in the class MethodDataCollection methods = c.Methods; // display the methods Console.WriteLine("Method Names: "); foreach (MethodData method in methods) { Console.WriteLine(method.Name); } Console.WriteLine(); // Get the properties in the class PropertyDataCollection properties = c.Properties; // display the properties Console.WriteLine("Property Names: "); foreach (PropertyData property in properties) { Console.WriteLine(property.Name); } Console.WriteLine(); // Get the Qualifiers in the class QualifierDataCollection qualifiers = c.Qualifiers; // display the qualifiers Console.WriteLine("Qualifier Names: "); foreach (QualifierData qualifier in qualifiers) { Console.WriteLine(qualifier.Name); } }
public QualifiersInfo(QualifierDataCollection qualifiers) { IsReadOnly = true; Description = string.Empty; foreach (var qd in qualifiers) { // class qualifiers if (qd.Name.Equals("Association")) { IsAssociation = true; } if (qd.Name.Equals("dynamic")) { IsDynamic = true; } if (qd.Name.ToUpperInvariant().Equals("GENERICPERFCTR")) { IsPerf = true; } if (qd.Name.Equals("SupportsUpdate")) { IsSupportsUpdate = true; } if (qd.Name.Equals("SupportsCreate")) { IsSupportsCreate = true; } if (qd.Name.Equals("SupportsDelete")) { IsSupportsDelete = true; } // property qualifiers if (qd.Name.Equals("CIM_Key")) { IsCimKey = true; } if (qd.Name.Equals("write")) { IsReadOnly = false; } if (qd.Name.Equals("Description")) { Description = qd.Value.ToString(); } } }
public static void Main() { // Get the WMI class ManagementScope s = new ManagementScope("\\\\MyBox\\root\\cimv2"); ManagementPath p = new ManagementPath("Win32_Environment"); ObjectGetOptions o = new ObjectGetOptions( null, System.TimeSpan.MaxValue, true); ManagementClass c = new ManagementClass(s, p, o); // Get the methods in the class MethodDataCollection methods = c.Methods; // display the methods Console.WriteLine("Method Names: "); foreach (MethodData method in methods) { Console.WriteLine(method.Name); } Console.WriteLine(); // Get the properties in the class PropertyDataCollection properties = c.Properties; // display the properties Console.WriteLine("Property Names: "); foreach (PropertyData property in properties) { Console.WriteLine(property.Name); } Console.WriteLine(); // Get the Qualifiers in the class QualifierDataCollection qualifiers = c.Qualifiers; // display the qualifiers Console.WriteLine("Qualifier Names: "); foreach (QualifierData qualifier in qualifiers) { Console.WriteLine(qualifier.Name); } }
static void Example() { // Get the WMI class ManagementClass processClass = new ManagementClass(); processClass.Path = new ManagementPath("Win32_Process"); // Get the methods in the class MethodDataCollection methods = processClass.Methods; // display the methods Console.WriteLine("Method Names: "); foreach (MethodData method in methods) { Console.WriteLine(method.Name); } Console.WriteLine(); // Get the properties in the class PropertyDataCollection properties = processClass.Properties; // display the properties Console.WriteLine("Property Names: "); foreach (PropertyData property in properties) { Console.Write("{0} = {1}", property.Name); } Console.WriteLine(); // Get the Qualifiers in the class QualifierDataCollection qualifiers = processClass.Qualifiers; // display the qualifiers Console.WriteLine("Qualifier Names: "); foreach (QualifierData qualifier in qualifiers) { Console.WriteLine(qualifier.Name); } }
public static int Main(string[] args) { ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk"); diskClass.Options.UseAmendedQualifiers = true; QualifierDataCollection diskQualifier = diskClass.Qualifiers; QualifierDataCollection.QualifierDataEnumerator qualifierEnumerator = diskQualifier.GetEnumerator(); while (qualifierEnumerator.MoveNext()) { Console.WriteLine( qualifierEnumerator.Current.Name + " = " + qualifierEnumerator.Current.Value); } return(0); }
public static void Main() { // Get the WMI class ManagementClass c = new ManagementClass("Win32_LogicalDisk"); // Get the methods in the class MethodDataCollection methods = c.Methods; // display the methods Console.WriteLine("Method Names: "); foreach (MethodData method in methods) { Console.WriteLine(method.Name); } Console.WriteLine(); // Get the properties in the class PropertyDataCollection properties = c.Properties; // display the properties Console.WriteLine("Property Names: "); foreach (PropertyData property in properties) { Console.WriteLine(property.Name); } Console.WriteLine(); // Get the Qualifiers in the class QualifierDataCollection qualifiers = c.Qualifiers; // display the qualifiers Console.WriteLine("Qualifier Names: "); foreach (QualifierData qualifier in qualifiers) { Console.WriteLine(qualifier.Name); } }
/// <summary> /// Collects the qualifier data /// </summary> /// <param name="collection">The qualifier collection</param> /// <returns>The lists with the data</returns> private static (List <string> DescriptionList, List <string> QualifierList) GetQualifierData( QualifierDataCollection collection) { var descriptionList = new List <string> { "Description:" }; var qualifierList = new List <string>() { "Qualifiers:" }; foreach (var entry in collection) { qualifierList.Add(entry.Name); if (entry.Name.EqualsIgnoreCase("description")) { descriptionList.Add(entry.Value.ToString()); } } return(descriptionList, qualifierList); }
private String GetQualiferDeclaration(QualifierDataCollection qualifiers) { if (qualifiers.Count == 0) { return(String.Empty); } int i = 0; var sb = new StringBuilder(); sb.Append("["); int count = 0; foreach (var qualifier in qualifiers) { if (IncludeQualifier(qualifier)) { count++; } } foreach (QualifierData qualifier in qualifiers) { if (IncludeQualifier(qualifier)) { sb.Append(qualifier.Name); // Append qualifer value if (qualifier.Value != null) { if (qualifier.Value.GetType() == typeof(Boolean)) { if (qualifier.Value == (Object)false) { sb.Append("(FALSE)"); } } else if (qualifier.Value.GetType().IsNumeric()) { sb.AppendFormat(@"({0})", qualifier.Value); } else { sb.Append(GetQualifierStringValue(qualifier, 0)); } } if (i < (count - 1)) { sb.Append(",\r\n "); } ; i++; } } sb.Append("]"); return(sb.ToString()); }
public static void LogicalDiskSample() { //// Управляющий класс ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk"); // true - если объекты, возвращенные от WMI, должны содержать // измененные сведения; diskClass.Options.UseAmendedQualifiers = true; //------- описание класса // Коллекция объектов QualifierData QualifierDataCollection qualifierCollection = diskClass.Qualifiers; // QualifierData - содержит сведения относительно WMI описателя foreach (QualifierData q in qualifierCollection) { Console.WriteLine(q.Name + " = " + q.Value); } Console.WriteLine(new string('-', 45)); //------------------------------------------------ //------- Доступные методы класса Console.WriteLine("\n Доступные методы класса... "); MethodDataCollection diskMethods = diskClass.Methods; foreach (MethodData method in diskMethods) { Console.WriteLine("Метод = " + method.Name); } Console.WriteLine(new string('-', 45)); //------------------------------------------------ // то же, но через нумератор //MethodDataCollection.MethodDataEnumerator diskEnumerator = // diskClass.Methods.GetEnumerator(); //while (diskEnumerator.MoveNext()) //{ // MethodData method = diskEnumerator.Current; // Console.WriteLine("Method = " + method.Name); //} //Console.WriteLine(new string('-', 45)); //------------------------------------------------- //------- Свойства класса ManagementObject diskС = new ManagementObject("Win32_LogicalDisk.DeviceID='C:'"); PropertyDataCollection.PropertyDataEnumerator propertyEnumerator = diskС.Properties.GetEnumerator(); while (propertyEnumerator.MoveNext()) { PropertyData p = (PropertyData)propertyEnumerator.Current; Console.WriteLine("Найденое свойство: " + p.Name); } Console.WriteLine(new string('-', 45)); //------------------------------------------------ //------- Экземпляры класса Console.WriteLine("\n Теперь отыщем все диски... "); // Получим коллекцию всех экземпляров класса ManagementObjectCollection disks = diskClass.GetInstances(); // Перечислитель коллекции ManagementObjectCollection.ManagementObjectEnumerator disksEnumerator = disks.GetEnumerator(); while (disksEnumerator.MoveNext()) { ManagementObject disk = (ManagementObject)disksEnumerator.Current; Console.WriteLine("Найден диск: " + disk["deviceid"]); Console.WriteLine("Размер: " + disk["Size"]); } Console.WriteLine(new string('-', 45)); //------------------------------------------------ // Получение сведений о методе класса MethodData m = diskClass.Methods["SetPowerState"]; Console.WriteLine("Name: " + m.Name); Console.WriteLine("Origin: " + m.Origin); // аргументы ManagementBaseObject inParams = m.InParameters; foreach (PropertyData pdata in inParams.Properties) { Console.WriteLine(); Console.WriteLine("InParam_Name: " + pdata.Name); Console.WriteLine("InParam_Type: " + pdata.Type); } // Возвращаемые значения ManagementBaseObject outParams = m.OutParameters; foreach (PropertyData pdata in outParams.Properties) { Console.WriteLine(); Console.WriteLine("OutParam_Name: " + pdata.Name); Console.WriteLine("OutParam_Type: " + pdata.Type); } }