如何在c#中获取pc的mac地址 [英] How to take mac address for a pc in c#

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

问题描述

如何获取我目前正在为网页设计工作的mac id(ASP.Net)

How to get mac id of my currently woking system for a web page designing (ASP.Net)

推荐答案

你好


查看此



使用c#在消息框中获取MAC地址[ ^ ]





谢谢



Magesh M
Hi
Check this

get MAC address in message box using c#[^]


Thanks

Magesh M


通过以下链接,在这里你会找到你的答案:



使用c#在消息框中获取MAC地址 [ ^ ]
go through the below link, here you will find your answer:

get MAC address in message box using c#[^]






Th有两种方法可以做到这一点。



1.)您需要导入System.Net命名空间才能生效。这将支持IPv4和IPv6

Hi,

There are two ways to do it.

1.) You need to import the System.Net namespace for this to work. This will support IPv4 and IPv6
public 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;
}





2.)要实现此功能,您需要在应用程序中添加System.Management命名空间。如果它不可用,您需要从项目的添加引用中添加其引用。



2.) To implement this function you need to add System.Management namespace in your application. If it is not available you need to add its reference from "Add references" from your project.

public string GetMACAddress()
{
    ManagementObjectSearcher objMOS = new ManagementObjectSearcher("Win32_NetworkAdapterConfiguration");
    ManagementObjectCollection objMOC = objMOS.Get();
    string MACAddress = String.Empty;
    foreach (ManagementObject objMO in objMOC)
    {
        if (MACAddress == String.Empty) // only return MAC Address from first card   
        {
            MACAddress = objMO["MacAddress"].ToString();
        }
        objMO.Dispose();
    }
    MACAddress = MACAddress.Replace(":", "");
    return MACAddress;
}







希望这有帮助!! :)



问候,

Praneet




Hope this helps !! :)

Regards,
Praneet


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

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