|
- 1、按钮事件
- private void button1_Click(object sender, EventArgs e)
- {
- textBox1.Text = GetMacByWMI()[0];
- }
- 2、一个方法搞定
- public List<string> GetMacByWMI()
- {
- List<string> macs = new List<string>();
- try
- {
- string mac = "";
- ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- if ((bool)mo["IPEnabled"])
- {
- mac = mo["MacAddress"].ToString();
- macs.Add(mac);
- }
- }
- moc = null;
- mc = null;
- }
- catch
- {
- }
-
- return macs;
- }
复制代码
|
|