WMI question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Shen

    #1

    WMI question

    Hi,

    Here are sections of my codes:

    private void test()
    {
    ObjectQuery objectQuery = new ObjectQuery("se lect * from
    Win32_NetworkAd apterConfigurat ion");
    ManagementObjec tSearcher searcher = new
    ManagementObjec tSearcher(objec tQuery);
    foreach (ManagementObje ct share in searcher.Get())
    {
    AppendObject(sh are);
    }

    }

    private void AppendObject(Ma nagementObject obj)
    {
    PropertyDataCol lection dataCollection = obj.Properties;
    foreach (PropertyData data in dataCollection)
    {
    if (data.IsArray)
    {
    object[] tempArray = (object[])(obj[data.Name]);
    if (tempArray != null)
    {
    AppendString(te xtBox1, data.Name);
    foreach (object temp in tempArray)
    {
    if (temp != null)
    Debug.WriteLine (temp.ToString( ));
    }
    }
    }
    else
    {
    object temp = obj[data.Name];
    if (temp != null)
    {
    AppendString(te xtBox1, data.Name + " = " + temp.ToString() );
    }
    else
    AppendString(te xtBox1, data.Name);
    }
    }
    AppendString(te xtBox1, "");
    }


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

    According to platform SDK, GatewayCostMetr ic 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.


  • Imran Koradia

    #2
    Re: WMI question

    > My question is why do I got an "System.Invalid CastException" when the code[color=blue]
    > reaches the line:
    > object[] tempArray = (object[])(obj[data.Name]);
    > when data.Name is GatewayCostMetr ic.
    >
    > According to platform SDK, GatewayCostMetr ic is an array of uint16 and the
    > in fact the Type property for the data.Type is CimType.UInt16.[/color]

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


    Imran.


    Comment

    Working...