gpt4 book ai didi

C#获取电脑MAC地址 "OFFLINE"

转载 作者:可可西里 更新时间:2023-11-01 08:53:47 28 4
gpt4 key购买 nike

在c#中没有互联网连接时,有没有办法获取计算机的mac地址?当我有连接时我能得到,但当我离线时我不能得到。但强烈要求我的工作需要 mac 地址。

我的在线代码;

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

最佳答案

来自 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;
}

来自 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;
}

略微修改自 How to get the MAC address of system - C-Sharp Corner

关于C#获取电脑MAC地址 "OFFLINE",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15783954/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com