给你我们写的一个例子吧
public string GetMacAddress()   //返回Mac地址  
{   
ManagementClass mc;
ManagementObjectCollection moc;
try
{        
mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
moc = mc.GetInstances(); 
}
catch
{
return "无法得到网卡物理地址!";
}
//****先得到网卡数目          
int i = 0; 
foreach(ManagementObject mo in moc) 
{                         
if((bool)mo["IPEnabled"] == true) 
{                
i++;                
}             
mo.Dispose(); 

//***赋值给数组 
ManagementClass mc_2 = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
ManagementObjectCollection moc_2 = mc_2.GetInstances(); 
string []array = new string[i]; 
int j = 0; 
foreach(ManagementObject mo in moc_2) 
{                         
if((bool)mo["IPEnabled"] == true) 

string temp = mo["MacAddress"].ToString(); 
temp = temp.Replace(":","-");                
array[j] = temp; 
j++;                
}             
mo.Dispose();  } 
string retv="";
foreach(string s in array)
retv+=s;
if(retv.Length>17)
{
retv=retv.Substring(0,17);
}
return retv;
}

解决方案 »

  1.   

    多谢 tavor(龙双公子) ,问题已经解决了
    不过我想问一下我的程序为什么会有问题?
    if((bool)mo["IPEnabled"] == true)
    这个条件判断的到底是什么??
      

  2.   

    The Win32_NetworkAdapterConfiguration WMI class represents the attributes and behaviors of a network adapter. This class has been extended to include extra properties and methods that support the management of the TCP/IP and Internetworking Packet Exchange (IPX) protocols that are independent of the network adapter.ipenabled is one of Win32_NetworkAdapterConfiguration's properties;here is its description.IPEnabled
    Data type: boolean
    Access type: Read-onlyIf TRUE, TCP/IP is bound and enabled on this network adapter.
      

  3.   

    (bool)mo["IPEnabled"] == true is used to check the mo adapter is effective or not.