Reliable Method to Get Machine'S MAC Address in C#

Getting MAC Address C#

i am using following code to access mac address in format you want :

public string GetSystemMACID()
{
string systemName = System.Windows.Forms.SystemInformation.ComputerName;
try
{
ManagementScope theScope = new ManagementScope("\\\\" + Environment.MachineName + "\\root\\cimv2");
ObjectQuery theQuery = new ObjectQuery("SELECT * FROM Win32_NetworkAdapter");
ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);
ManagementObjectCollection theCollectionOfResults = theSearcher.Get();

foreach (ManagementObject theCurrentObject in theCollectionOfResults)
{
if (theCurrentObject["MACAddress"] != null)
{
string macAdd = theCurrentObject["MACAddress"].ToString();
return macAdd.Replace(':', '-');
}
}
}
catch (ManagementException e)
{
}
catch (System.UnauthorizedAccessException e)
{

}
return string.Empty;
}

C# Get Computer's MAC address OFFLINE

From WMI:

public static string GetMACAddress1()
{
ManagementObjectSearcher objMOS = new ManagementObjectSearcher("Select * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMOS.Get();
string macAddress = String.Empty;
foreach (ManagementObject objMO in objMOC)
{
object tempMacAddrObj = objMO["MacAddress"];

if (tempMacAddrObj == null) //Skip objects without a MACAddress
{
continue;
}
if (macAddress == String.Empty) // only return MAC Address from first card that has a MAC Address
{
macAddress = tempMacAddrObj.ToString();
}
objMO.Dispose();
}
macAddress = macAddress.Replace(":", "");
return macAddress;
}

From System.Net namespace:

public static string GetMACAddress2()
{
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(); Line is not required
sMacAddress = adapter.GetPhysicalAddress().ToString();
}
} return sMacAddress;
}

Slightly modified from How to get the MAC address of system - C-Sharp Corner

C# get local MAC address by local IP (multiple interfaces)

Here is a proposed solution that does what you are asking for:

void Main()
{
Console.WriteLine(GetMacByIP("192.168.15.161")); // will return "00-E2-4C-98-18-89"
}

public string GetMacByIP(string ipAddress)
{
// grab all online interfaces
var query = NetworkInterface.GetAllNetworkInterfaces()
.Where(n =>
n.OperationalStatus == OperationalStatus.Up && // only grabbing what's online
n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.Select(_ => new
{
PhysicalAddress = _.GetPhysicalAddress(),
IPProperties = _.GetIPProperties(),
});

// grab the first interface that has a unicast address that matches your search string
var mac = query
.Where(q => q.IPProperties.UnicastAddresses
.Any(ua => ua.Address.ToString() == ipAddress))
.FirstOrDefault()
.PhysicalAddress;

// return the mac address with formatting (eg "00-00-00-00-00-00")
return String.Join("-", mac.GetAddressBytes().Select(b => b.ToString("X2")));
}

In C# how to find the mac address of internal devices not the Router mac address

This method is the best way to get Mac address of your User.

[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
private static string GetClientMAC(string strClientIP)
{
string mac_dest = "";
try
{
Int32 ldest = inet_addr(strClientIP);
Int32 lhost = inet_addr("");
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest, 0, ref macinfo, ref len);
string mac_src = macinfo.ToString("X");

while (mac_src.Length < 12)
{
mac_src = mac_src.Insert(0, "0");
}

for (int i = 0; i < 11; i++)
{
if (0 == (i % 2))
{
if (i == 10)
{
mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
}
else
{
mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
}
}
}
}
catch (Exception err)
{
throw new Exception("Lỗi " + err.Message);
}
return mac_dest;
}

Get a machines MAC address on the local network from its IP in C#

As per Marco Mp's comment above, have used ARP tables. arp class

get MAC address of computer

When you disable your network adapter, you can't access it at all - it is as if it isn't installed, which is why you don't see a MAC address.

EDIT: Explanation:

A MAC address belongs to a network adapter. If you have 3 adapters you have 3 MAC addresses. If you have no adapters, you have no MAC address.



Related Topics



Leave a reply



Submit