Click here to Skip to main content
15,886,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:-

C#
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
Posted
Updated 23-Jul-14 19:40pm
v2

 
Share this answer
 
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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900