代碼如下:
using?System;using?System.Drawing;
using?System.Windows.Forms;
using?System.Management;?//必須在項目中添加System.Management引用!
using?System.Collections.Generic;
namespace?WindowsFormsApplication1
{
public?partial?class?Form1?:?Form
{
public?Form1()
{
InitializeComponent();
comboBox1.DropDownStyle?=?ComboBoxStyle.DropDownList;
comboBox1.Items.AddRange(GetNetcardName().ToArray());
}
//?獲取計算機上安裝的網卡名稱
private?List<string>?GetNetcardName()
{
List<string>?cardNames?=?new?List<string>();
ManagementClass?mc?=?
new?ManagementClass("Win32_NetworkAdapter");
ManagementObjectCollection?moc?=?mc.GetInstances();
foreach?(ManagementObject?mo?in?moc)
{
cardNames.Add(mo["Description"].ToString());}
return?cardNames;
}
}
}