C#获取计算机的MAC地址" OFFLINE" [英] C# Get Computer's MAC address "OFFLINE"

查看:209
本文介绍了C#获取计算机的MAC地址" OFFLINE"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法让计算机的MAC地址时,有在C#中没有互联网连接?
我'能够得到当我有联系,但不是能够得到当我下线。但我强烈需要的MAC地址我的工作。

我在网上code;

  VAR MACADDR =
      (NIC从在NetworkInterface.GetAllNetworkInterfaces()
       其中,nic.OperationalStatus == OperationalStatus.Up
       选择nic.GetPhysicalAddress()的ToString())FirstOrDefault()。


解决方案

从WMI:

 公共静态字符串GetMACAddress1()
{
    ManagementObjectSearcher objMOS =新ManagementObjectSearcher(SELECT * FROM Win32_NetworkAdapterConfiguration的);
    ManagementObjectCollection objMOC = objMOS.Get();
    字符串MACADDRESS =的String.Empty;
    的foreach(的ManagementObject objMO在objMOC)
    {
        反对tempMacAddrObj = objMO [MACADDRESS];        如果(tempMacAddrObj == NULL)//跳过对象没有MACADDRESS
        {
            继续;
        }
        如果(MACADDRESS ==的String.Empty)//只从具有MAC地址第一卡返回MAC地址
        {
            MACADDRESS = tempMacAddrObj.ToString();
        }
        objMO.Dispose();
    }
    MACADDRESS = macAddress.Replace(:,);
    返回MACADDRESS;
}

从System.Net命名空间:

 公共静态字符串GetMACAddress2()
{
    的NetworkInterface [] =网卡NetworkInterface.GetAllNetworkInterfaces();
    字符串sMacAddress =的String.Empty;
    的foreach(在网卡的NetworkInterface适配器)
    {
        如果(sMacAddress ==的String.Empty)//只能从第一张卡返回MAC地址
        {
            // IPInterfaceProperties属性= adapter.GetIPProperties();线不需要
            sMacAddress = adapter.GetPhysicalAddress()的ToString()。
        }
    }返回sMacAddress;
}

略低于<修改href=\"http://www.c-sharpcorner.com/uploadfile/ahsanm.m/how-to-get-the-mac-address-of-system-using-Asp-NetC-Sharp/\">How获取系统的MAC地址 - C-尖角

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.

My online code;

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

解决方案

From 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:

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

这篇关于C#获取计算机的MAC地址&QUOT; OFFLINE&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆