使用c#获取MAC地址 [英] Get MAC Address using c#

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

问题描述





任何人都可以提供获取MAC地址的片段。



现在我尝试了很多片段,但它们都运行不好



Snippet-1:

Hi,

Can any one provide me the snippet to get the MAC Address.

Right now I tried a lot of snippet but they all not worked well

Snippet-1 :

public static string GetMacAddress()
{
    string macAddresses = "";

    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        if (nic.OperationalStatus == OperationalStatus.Up)
        {
            macAddresses += nic.GetPhysicalAddress().ToString();
            break;
        }
    }
    return macAddresses;
}





它在我的系统中返回空字符串



Snippet-2:



It returns empty string in my system

Snippet-2 :

public static string GetMacAddress()
        {
            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();
                    sMacAddress = adapter.GetPhysicalAddress().ToString();
                }
            }
            return sMacAddress;
        }





它还会返回空字符串



片段-3:



It also returns empty string

Snippet-3 :

public static PhysicalAddress GetMacAddress()
{
    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        // Only consider Ethernet network interfaces
        if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
            nic.OperationalStatus == OperationalStatus.Up)
        {
            return nic.GetPhysicalAddress();
        }
    }
    return null;
}





它只适用于以太网,我的是ppp



任何人都可以提供我的代码片段来查找适用于每台电脑的MAC地址



在此先感谢



It only works for Ethernet network, mine is ppp

Can any one provide me snippet to find MAC Address which works for every computer

Thanks In Advance

推荐答案

using System;
using System.Linq;
using System.Net.NetworkInformation;

namespace ConsoleApplication1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            foreach (var nic in NetworkInterface.GetAllNetworkInterfaces().Where(nic => nic.OperationalStatus == OperationalStatus.Up)) {
                Console.WriteLine("{0} has an address of {1}", nic.NetworkInterfaceType, nic.GetPhysicalAddress());
            }
            Console.ReadKey();
        }
    }
}





此控制台应用程序返回所有已启动的接口。 。





This console app returns all the interfaces that are up...

private static string[] GetUpInterfaces()
        {
            return
                NetworkInterface.GetAllNetworkInterfaces()
                    .Where(nic => nic.OperationalStatus == OperationalStatus.Up)
                    .Select(xx => xx.GetPhysicalAddress().ToString()).ToArray();
        }


这篇关于使用c#获取MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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