C# 获取计算机的 MAC 地址 "OFFLINE"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15783954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Get Computer's MAC address "OFFLINE"
提问by Mehmet Ince
Is there any way to get computer's mac address when there is no internet connection in c#? I'am able to get when I have connection but not able to get when I am offline. But strongly I need the mac address for my work.
c#没有互联网连接时,有没有办法获取计算机的mac地址?我有连接时可以访问,但离线时无法访问。但我强烈需要我的工作的 mac 地址。
My online code;
我的在线代码;
var macAddr =
(from nic in NetworkInterface.GetAllNetworkInterfaces()
where nic.OperationalStatus == OperationalStatus.Up
select nic.GetPhysicalAddress().ToString()).FirstOrDefault();
采纳答案by jordanhill123
From WMI:
来自 WMI:
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;
}
From System.Net namespace:
从 System.Net 命名空间:
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;
}
Slightly modified from How to get the MAC address of system - C-Sharp Corner
回答by PhonicUK
You can use WMI in C# (System.Management
) to get a list of Win32_NetworkAdapter
's which contains a MACAddress
property.
您可以在 C# ( System.Management
) 中使用 WMI来获取Win32_NetworkAdapter
包含MACAddress
属性的's列表。
http://msdn.microsoft.com/en-gb/library/windows/desktop/aa394216(v=vs.85).aspx
http://msdn.microsoft.com/en-gb/library/windows/desktop/aa394216(v=vs.85).aspx