2015/01/29

C#.Net 使用WMI取得網路卡資訊

如果你想知道網路卡有哪些資訊可以參考『Win32_NetworkAdapter class

Code:
using System;
using System.Windows.Forms;
using System.Linq;
using System.Management;

namespace Sample
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

            ManagementObjectSearcher search = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter");
            ManagementObjectCollection collection = search.Get();
            var networkList = from n in collection.Cast<ManagementBaseObject>()
                              select new
                              {
                                  guid = n.GetPropertyValue("GUID"),
                                  name = n.GetPropertyValue("Name"),
                                  mac = n.GetPropertyValue("MACAddress")
                                 
                              };
            foreach (var n in networkList)
                textBox1.Text += String.Format("{0}{2}{1}{2}{2}",n.name,n.mac, Environment.NewLine);

        }

    }

}

執行結果:


參考資料:
https://msdn.microsoft.com/en-us/library/aa394216(v=vs.85).aspx