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)
{
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