开发者

WMI Win32_DiskDrive Capabilities Property - Convert UInt16 to String

开发者 https://www.devze.com 2022-12-21 20:48 出处:网络
In Win32_DiskDrive. There\'s a Capabilities Property which has a type of System.UInt16[]. Am trying to get the values by using GetProperty and convert it to string. But it keeps throwing an error (I

In Win32_DiskDrive. There's a Capabilities Property which has a type of System.UInt16[].

Am trying to get the values by using GetProperty and convert it to string. But it keeps throwing an error (I don't want to trap it) on Capabilities property.

The error was: InvalidCastException

Message: Object must implement IConvertible.开发者_运维知识库


There's the WMI Code Creator tool from Microsoft that can generate C#, Visual Basic .NET and VBScript code for you to run any WMI query and enumerate the results. It's also very useful for exploring WMI namespaces and classes, so it's a must-have when dealing with WMI.

Now back to the question. From the System.UInt16[] syntax I assume that you're using C#. Here's sample C# code (generated by WMI Code Creator, with a few minor changes) that demonstrates how you can access individual elements of the Capabilities array:

ManagementObjectSearcher searcher = 
    new ManagementObjectSearcher("root\\CIMV2", 
    "SELECT * FROM Win32_DiskDrive"); 

foreach (ManagementObject queryObj in searcher.Get())
{
    if(queryObj["Capabilities"] == null)
        Console.WriteLine("Capabilities: {0}", queryObj["Capabilities"]);
    else
    {
        UInt16[] arrCapabilities = (UInt16[])(queryObj["Capabilities"]);
        foreach (UInt16 arrValue in arrCapabilities)
        {
            Console.WriteLine("Capabilities: {0}", arrValue);
        }
    }
    Console.WriteLine();
}

To convert a UInt16 value to a string, you can use the ToString method, for example:

foreach (UInt16 arrValue in arrCapabilities)
{
    Console.WriteLine(arrValue.ToString());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号