如何从Win32_CDROMDrive WMI类获取接口类型?

时间:2013-12-18 21:35:20

标签: c# .net wmi

我已经实现了Win32_DiskDrive的目标,但在Win32_CDROMDrive WMI类中没有该属性( InterfaceType )。

如何获取光驱的接口类型?还有其他手段可以获得它吗?

提前致谢。

1 个答案:

答案 0 :(得分:2)

您可以从DeviceIDPNPDeviceID属性中提取界面类型。

试试这个样本

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                ManagementScope Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", "localhost"), null);
                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_CDROMDrive");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    String InterfaceType =  ((String)WmiObject["DeviceID"]).Substring(0, ((String)WmiObject["DeviceID"]).IndexOf(@"\"));
                    Console.WriteLine("{0,-35} {1,-40}", "InterfaceType", InterfaceType);
                    Console.WriteLine("{0,-35} {1,-40}","Drive",WmiObject["Drive"]);
                    Console.WriteLine("{0,-35} {1,-40}","Name",WmiObject["Name"]);

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}