日期:2014-05-19  浏览次数:21141 次

如果通过网络连接名称得到网卡名称或者通过网卡名称得到(100分急求)
c#   如何通过“本地连接”得到对应的网卡名称。
或者通过网卡   找到对应的   “本地连接”名称!



------解决方案--------------------
using System.Management;
...
ManagementClass mc = new ManagementClass( "Win32_NetworkAdapterConfiguration ");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if((bool)mo[ "IPEnabled "] == true)
Console.WriteLine( "MAC address\t{0} ", mo[ "MacAddress "].ToString());
mo.Dispose();
}
}
------解决方案--------------------
尝试使用WMI方式获取:

private void button1_Click(object sender, System.EventArgs e)
{
string strsearch=string.Format(@ "SELECT MACAddress FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL)AND (Manufacturer <> 'Microsoft ')) ");

System.Management.ManagementObjectSearcher msearch=new ManagementObjectSearcher();
msearch.Scope=new ManagementScope(@ "root\CIMV2 ");
msearch.Query=new ObjectQuery(strsearch);
int i=1;
try
{
if((msearch.Get().Count <1) || (msearch.Get()==null))
{
this.listBox1.Items.Add( "No MAC Address! ");
}
else
{
foreach(System.Management.ManagementObject mymac in msearch.Get())
{
string str1=string.Format( "MAC{0} address:{1} ",i.ToString(),mymac[ "MACAddress "].ToString());
this.listBox1.Items.Add(str1);
i++;
}
}
}
catch(Exception ex)
{
string msg=ex.Message.ToString();
MessageBox.Show( "erro ", msg);
}

}
------解决方案--------------------
WMI
using System.Management;
//get infomation of MAC
//get infomation of MAC
myinfo1= new ManagementClass( "WIN32_NetworkAdapterConfiguration ");
my=myinfo1.GetInstances();
foreach( ManagementObject one in my)
{
if((bool)one[ "IPEnabled "]==true)
{
info=one[ "MacAddress "].ToString();
this.textBox2.Text=info;
break;
}

}
------解决方案--------------------
http://book.hackbase.com/data/web5004/20050417/20050417__3879994.html