none
geeting error while creating ManagementObject class's object ...error is : specified argument was out of the range of valid values. parameter name path wmi RRS feed

  • Question

  • I am doing same as shown on the MSDN site  http://msdn.microsoft.com/en-us/library/ms143622.aspx

    .but i error is comming

    public static int Main(string[] args) 
        {
            ManagementScope s = new ManagementScope("\\\\.\\root\\cimv2");
            ManagementPath p = new ManagementPath("Win32_Service");
    
            // Set options for no context info, 
            // but requests amended qualifiers  
            // to be contained in the object
            ObjectGetOptions opt = new ObjectGetOptions(null, TimeSpan.MaxValue, true); 
    
            ManagementObject o = new ManagementObject(s, p, opt);
    
            Console.WriteLine(o.Qualifiers["Description"].Value);
    
            return 0;
        }
    what to do ?
    Thursday, August 1, 2013 8:36 PM

Answers

  • It guess there's an error in the MSDN sample.

    "Win32_Service" is a class, so you cannot create a ManagementObject out of it. You must create a ManagementClass. The following code should work:

    ManagementScope scope = new ManagementScope(@"\\.\root\cimv2");
    ManagementPath path = new ManagementPath("Win32_Service");
    ObjectGetOptions options = new ObjectGetOptions(null, TimeSpan.MaxValue, true);
    ManagementClass obj = new ManagementClass(scope, path, options);
    
    Console.WriteLine(obj.Qualifiers ["Description"].Value);
    

    This will obviously print the description of the class, not of any specific service.

    Conversely, if you try to access a specific instance (e.g. using "Win32_Service.Name='Spooler'"), you must create a ManagementObject, e.g.

    ManagementScope scope = new ManagementScope(@"\\.\root\cimv2");
    ManagementPath path = new ManagementPath("Win32_Service.Name='Spooler'");
    ObjectGetOptions options = new ObjectGetOptions(null, TimeSpan.MaxValue, true);
    ManagementObject obj = new ManagementObject(scope, path, options);
    
    Console.WriteLine(obj.Properties ["Description"].Value);
    

    HTH
    --mc

    Friday, August 2, 2013 4:12 PM

All replies

  • Try using a query instead:

    ManagementScope scope = new ManagementScope(@"\\.\root\cimv2");
    ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Service");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
          
    foreach (var item in searcher.Get()) {
      Console.WriteLine(item.Properties ["Description"].Value);
    }
    
    HTH
    --mc

    Thursday, August 1, 2013 11:45 PM
  • it's ok...

    but why that code not working ?

    what's wrong ? 

    Friday, August 2, 2013 3:26 AM
  • It guess there's an error in the MSDN sample.

    "Win32_Service" is a class, so you cannot create a ManagementObject out of it. You must create a ManagementClass. The following code should work:

    ManagementScope scope = new ManagementScope(@"\\.\root\cimv2");
    ManagementPath path = new ManagementPath("Win32_Service");
    ObjectGetOptions options = new ObjectGetOptions(null, TimeSpan.MaxValue, true);
    ManagementClass obj = new ManagementClass(scope, path, options);
    
    Console.WriteLine(obj.Qualifiers ["Description"].Value);
    

    This will obviously print the description of the class, not of any specific service.

    Conversely, if you try to access a specific instance (e.g. using "Win32_Service.Name='Spooler'"), you must create a ManagementObject, e.g.

    ManagementScope scope = new ManagementScope(@"\\.\root\cimv2");
    ManagementPath path = new ManagementPath("Win32_Service.Name='Spooler'");
    ObjectGetOptions options = new ObjectGetOptions(null, TimeSpan.MaxValue, true);
    ManagementObject obj = new ManagementObject(scope, path, options);
    
    Console.WriteLine(obj.Properties ["Description"].Value);
    

    HTH
    --mc

    Friday, August 2, 2013 4:12 PM