2013-04-03 19 views
9

C# içinde internet bağlantısı olmadığında bilgisayarın mac adresini almanın bir yolu var mı? Bağlantı kurulduğunda alabiliyorum ancak çevrimdışı olduğumda bulamıyorum. Ama kesinlikle işim için mac adresine ihtiyacım var.C# Bilgisayarınızın MAC adresini "OFFLINE" edinin

Çevrimiçi kodum;

var macAddr = 
     (from nic in NetworkInterface.GetAllNetworkInterfaces() 
     where nic.OperationalStatus == OperationalStatus.Up 
     select nic.GetPhysicalAddress().ToString()).FirstOrDefault(); 
+0

değiştirilmiş

public static string GetMACAddress2() { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); String sMacAddress = string.Empty; foreach (NetworkInterface adapter in nics) { if (sMacAddress == String.Empty)// only return MAC Address from first card { //IPInterfaceProperties properties = adapter.GetIPProperties(); Line is not required sMacAddress = adapter.GetPhysicalAddress().ToString(); } } return sMacAddress; } 

'nerede nic.OperationalStatus = = OperationalStatus.Up' satırı? – Pondidum

+0

Online olduğunda, mac adresi; 4CEB428D5072 Çevrimdışı olduğunda, mac adresi 4CEB428D5073. Niye ya? –

cevap

24

: System.Net ad itibaren

public static string GetMACAddress1() 
{ 
    ManagementObjectSearcher objMOS = new ManagementObjectSearcher("Select * FROM Win32_NetworkAdapterConfiguration"); 
    ManagementObjectCollection objMOC = objMOS.Get(); 
    string macAddress = String.Empty; 
    foreach (ManagementObject objMO in objMOC) 
    { 
     object tempMacAddrObj = objMO["MacAddress"]; 

     if (tempMacAddrObj == null) //Skip objects without a MACAddress 
     { 
      continue; 
     } 
     if (macAddress == String.Empty) // only return MAC Address from first card that has a MAC Address 
     { 
      macAddress = tempMacAddrObj.ToString();    
     } 
     objMO.Dispose(); 
    } 
    macAddress = macAddress.Replace(":", ""); 
    return macAddress; 
} 

: Biraz sadece kaldırabilirsiniz How to get the MAC address of system - C-Sharp Corner

+0

Satır IPInterfaceProperties özellikleri = adapter.GetIPProperties(); 'gerekli mi? – Joel

+0

@Joel Hayır, bu satıra gerek yok tabanlı testler sadece dev kutumda koştum. Testimi yansıtacak güncellenmiş cevap. – jordanhill123