How do I do this in C#?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have been using the following code in a vbs script:

This is a WMI query and I have omitted the connection strings...

set Chassis = objService.ExecQuery("Select ChassisTypes from
Win32_SystemEnclosure Where tag = 'system enclosure 0'")
for each item in Chassis
rs("ChassisType") = item.chassistypes(0)
next


This query returns a collection of values and I only want the first one.
"item.chassistypes(0)"

I want to convert this to C# however I am having some trouble figuring out
how to work through the object. Here is what I have so far...

ManagementObjectCollection collection = searcher.Get();

foreach (ManagementObject m in collection)
{
_chassisType = m[@"ChassisTypes(0)"].ToString();
}

This generate the error:

System.Management.ManagementException: Not found
at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus
errorCode)
at System.Management.PropertyData.RefreshPropertyInfo()
at System.Management.PropertyDataCollection.get_Item(String propertyName)
at System.Management.ManagementBaseObject.GetPropertyValue(String
propertyNam e)
at System.Management.ManagementBaseObject.get_Item(String propertyName)
at FirstInv.Chassis.Get(String name) in C:\... Line 41

If possible could you point me in the right direction?

Thanks!
 
James said:
I have been using the following code in a vbs script:

This is a WMI query and I have omitted the connection strings...

set Chassis = objService.ExecQuery("Select ChassisTypes from
Win32_SystemEnclosure Where tag = 'system enclosure 0'")
for each item in Chassis
rs("ChassisType") = item.chassistypes(0)
next


This query returns a collection of values and I only want the first one.
"item.chassistypes(0)"

I want to convert this to C# however I am having some trouble figuring out
how to work through the object. Here is what I have so far...

ManagementObjectCollection collection = searcher.Get();

foreach (ManagementObject m in collection)
{
_chassisType = m[@"ChassisTypes(0)"].ToString();
}

This generate the error:

System.Management.ManagementException: Not found
at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus
errorCode)
at System.Management.PropertyData.RefreshPropertyInfo()
at System.Management.PropertyDataCollection.get_Item(String
propertyName)
at System.Management.ManagementBaseObject.GetPropertyValue(String
propertyNam e)
at System.Management.ManagementBaseObject.get_Item(String propertyName)
at FirstInv.Chassis.Get(String name) in C:\... Line 41

If possible could you point me in the right direction?

Thanks!


Try this...

foreach (ManagementObject m in collection)
{
short[] chtype = m.Properties["ChassisTypes"].Value as short[];
chassisType = chtype[0];

Willy.
 
Willy Denoyette said:
James said:
I have been using the following code in a vbs script:

This is a WMI query and I have omitted the connection strings...

set Chassis = objService.ExecQuery("Select ChassisTypes from
Win32_SystemEnclosure Where tag = 'system enclosure 0'")
for each item in Chassis
rs("ChassisType") = item.chassistypes(0)
next


This query returns a collection of values and I only want the first one.
"item.chassistypes(0)"

I want to convert this to C# however I am having some trouble figuring out
how to work through the object. Here is what I have so far...

ManagementObjectCollection collection = searcher.Get();

foreach (ManagementObject m in collection)
{
_chassisType = m[@"ChassisTypes(0)"].ToString();
}

This generate the error:

System.Management.ManagementException: Not found
at
System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus
errorCode)
at System.Management.PropertyData.RefreshPropertyInfo()
at System.Management.PropertyDataCollection.get_Item(String
propertyName)
at System.Management.ManagementBaseObject.GetPropertyValue(String
propertyNam e)
at System.Management.ManagementBaseObject.get_Item(String propertyName)
at FirstInv.Chassis.Get(String name) in C:\... Line 41

If possible could you point me in the right direction?

Thanks!


Try this...

foreach (ManagementObject m in collection)
{
short[] chtype = m.Properties["ChassisTypes"].Value as short[];
chassisType = chtype[0];

Willy.

Thanks Willy! That is what I needed... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top