3

C# を使用して、ネットワーク アダプターのハードウェア ID を照会する必要があります。

System.Management を使用すると、デバイス ID、説明などの詳細を照会できますが、ハードウェア ID は照会できません。

ここで、listBox1 は、winform アプリでアイテムを表示するための単純なリストボックス コントロール インスタンスです。

例:

ManagementObjectCollection mbsList = null;
ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_NetworkAdapter");
                mbsList = mbs.Get();
                foreach (ManagementObject mo in mbsList)
                {
                    listBox1.Items.Add("Name : " + mo["Name"].ToString());
                    listBox1.Items.Add("DeviceID : " + mo["DeviceID"].ToString());
                    listBox1.Items.Add("Description : " + mo["Description"].ToString());
                }

ただし、MSDN WMI リファレンスを見ると、HardwareId を取得する方法がありません。devcon ツール ( devcon hwids =net ) を使用すると、各デバイスが HardwareId に関連付けられていることがわかります

どんな助けでも大歓迎です

4

1 に答える 1

3

探している HardwareID は、別の WMI クラスにあります。Win32_NetworkAdapeter のインスタンスを取得したら、PNPDeviceId を使用して Win32_PnpEntry を選択できます。すべてのネットワーク アダプタとそのハードウェア ID (存在する場合) を一覧表示するサンプル コードを次に示します。

        ManagementObjectSearcher adapterSearch = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_NetworkAdapter");

        foreach (ManagementObject networkAdapter in adapterSearch.Get())
        {
            string pnpDeviceId = (string)networkAdapter["PNPDeviceID"];
            Console.WriteLine("Description  : {0}", networkAdapter["Description"]);
            Console.WriteLine(" PNPDeviceID : {0}", pnpDeviceId);

            if (string.IsNullOrEmpty(pnpDeviceId))
                continue;

            // make sure you escape the device string
            string txt = "SELECT * FROM win32_PNPEntity where DeviceID='" + pnpDeviceId.Replace("\\", "\\\\") + "'";
            ManagementObjectSearcher deviceSearch = new ManagementObjectSearcher("root\\CIMV2", txt);
            foreach (ManagementObject device in deviceSearch.Get())
            {
                string[] hardwareIds = (string[])device["HardWareID"];
                if ((hardwareIds != null) && (hardwareIds.Length > 0))
                {
                    Console.WriteLine(" HardWareID: {0}", hardwareIds[0]);
                }
            }
        }
于 2011-09-20T06:20:02.000 に答える