WMI question

G

Guest

Hi,

Here are sections of my codes:

private void test()
{
ObjectQuery objectQuery = new ObjectQuery("select * from
Win32_NetworkAdapterConfiguration");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(objectQuery);
foreach (ManagementObject share in searcher.Get())
{
AppendObject(share);
}

}

private void AppendObject(ManagementObject obj)
{
PropertyDataCollection dataCollection = obj.Properties;
foreach (PropertyData data in dataCollection)
{
if (data.IsArray)
{
object[] tempArray = (object[])(obj[data.Name]);
if (tempArray != null)
{
AppendString(textBox1, data.Name);
foreach (object temp in tempArray)
{
if (temp != null)
Debug.WriteLine(temp.ToString());
}
}
}
else
{
object temp = obj[data.Name];
if (temp != null)
{
AppendString(textBox1, data.Name + " = " + temp.ToString());
}
else
AppendString(textBox1, data.Name);
}
}
AppendString(textBox1, "");
}


My question is why do I got an "System.InvalidCastException" when the code
reaches the line:
object[] tempArray = (object[])(obj[data.Name]);
when data.Name is GatewayCostMetric.

According to platform SDK, GatewayCostMetric is an array of uint16 and the
in fact the Type property for the data.Type is CimType.UInt16.

I have trace through and be able to cast array of strings to array of
objects. Why is this cast invalid?

Any one can help me on this? Thanks.
 
I

Imran Koradia

My question is why do I got an "System.InvalidCastException" when the code
reaches the line:
object[] tempArray = (object[])(obj[data.Name]);
when data.Name is GatewayCostMetric.

According to platform SDK, GatewayCostMetric is an array of uint16 and the
in fact the Type property for the data.Type is CimType.UInt16.

True - its a uint16 array. But its the 'Value' thats an array - not the
name. The 'Name' is a String property and is simply 'GatewayCostMetric' in
this case - just one value not an array which is why you get an
InvalidCastException.


Imran.
 

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

Top