为什么我们需要使用c#.net获取MAC地址的网络连接? [英] Why we need network connectivity for Get MAC Address using c# .net?

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

问题描述

您好,



为什么我们需要使用c#.net获取MAC地址的LAN网络连接?



当我禁用LAN网络时,没有获得MAC地址,为什么?



我想获得没有LAN网络的MAC地址,是否可能?



我也使用过两个代码: -

首先: -

public static String GetMACAddress()

{

#region获取MAC地址

尝试

{

ManagementClass mc = new ManagementClass( Win32_NetworkAdapterConfiguration);

ManagementObjectCollection moc = mc.GetInstances();

string MACAddress = String.Empty;

foreach(ManagementObject mo in moc)

{

if(MACAddress == String.Empty)//仅从第一张卡返回MAC地址

{

if((bool)mo [IPEnabled] == true)MACAddress = mo [MacAddress]。ToString();

}

mo.Dispose();

}



MACAddress = MACAddress.Replace(:,);

返回MACAddress;

}

catch(例外情况)

{

抛出新例外(ex .Message);

}

#endregion

}



第二名: -



  public   string  GetMACAddress()
{
NetworkInterface [] nics = NetworkInterface.GetAllNetworkInterfaces();
字符串 sMacAddress = 字符串 .Empty;
foreach (NetworkInterface adapter in nics)
{
if (sMacAddress == String .Empty) // 仅从第一张卡返回MAC地址
{
IPInterfaceProperties properties = adapter.GetIPProperties();
sMacAddress = adapter.GetPhysicalAddress()。ToString();
}
} return sMacAddress;
}





但我的目的不是解决。



请帮帮我。



先谢谢。



Ankit Agarwal

软件工程师

解决方案

我希望这能解决你的问题



http://www.c- sharpcorner.com/uploadfile/ahsanm.m/how-to-get-the-mac-address-of-system-using-Asp-NetC-Sharp/ [ ^ ]





问候

Nakul Kundra


public static String GetMacAddress()

{

String MacAdd = String.Empty;

foreach(NetworkInterf中的NetworkInterface nic) ace.GetAllNetworkInterfaces())

{

//只考虑以太网网络接口

if(nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&

nic.OperationalStatus == OperationalStatus.Down)//非连接网络

{

MacAdd = Convert.ToString(nic.GetPhysicalAddress()) ;

返回MacAdd;

}

}

返回MacAdd;

}

Hello,

Why we need LAN network connectivity for Get MAC Address using c# .net?

When i disabled LAN network so, did not get MAC Address, Why?

I want to Get MAC Address without LAN Network, is it Possible?

I have used both code also:-
First:-
public static String GetMACAddress()
{
#region Get MAC Address
try
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
string MACAddress = String.Empty;
foreach (ManagementObject mo in moc)
{
if (MACAddress == String.Empty) // only return MAC Address from first card
{
if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
}
mo.Dispose();
}

MACAddress = MACAddress.Replace(":", "");
return MACAddress;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
#endregion
}

Second:-

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



But my purpose is not solving.

Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer

解决方案

I hope this will solve your problem

http://www.c-sharpcorner.com/uploadfile/ahsanm.m/how-to-get-the-mac-address-of-system-using-Asp-NetC-Sharp/[^]


Regards
Nakul Kundra


public static String GetMacAddress()
{
String MacAdd = String.Empty;
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
// Only consider Ethernet network interfaces
if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
nic.OperationalStatus == OperationalStatus.Down) //non connection network
{
MacAdd = Convert.ToString(nic.GetPhysicalAddress());
return MacAdd;
}
}
return MacAdd;
}


这篇关于为什么我们需要使用c#.net获取MAC地址的网络连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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