0

I need to get the MAC address of my router in order to use it like identifier in my program.

My PC is connected to the router using Ethernet cable

Is there a way to fetch the MAC, and if so is there a way to change it or clone it using C#?

I need to get the router MAC address not my network card MAC address ...

I saw a seed like this Get BSSID (MAC address) of wireless access point from C# ... if it is possible to get the MAC address of a wireless device, isn't it possible to get the MAC address of a connected Ethernet device?

Community
  • 1
  • 1
Yasser
  • 1,725
  • 4
  • 22
  • 28
  • @MariusBancila I need to get the router Mac address not to get my network card Mac adress – Yasser Apr 29 '13 at 10:02
  • @BrianO''Byrne sorry I didn't get the idea it's too complex now if I know my router IP address and I only need to get the router MAC address how do I do it please provide an example – Yasser Apr 29 '13 at 12:48
  • 1
    Yes, it is complex. Run `arp -a` from the command line and see the output it gives you. This will include the MAC address of your router. Now do the same thing from your C# code. Start a shell, run arp, and parse the results. Or, use the Windows API defined in the linked answer. That is as much help as I can give you. – Brian O''Byrne Apr 29 '13 at 12:58
  • 2
    Vote to reopen: The question is NOT a duplicate. – Michael Liu May 01 '13 at 00:22

2 Answers2

1

You may seek into this answer: Get the Default Gateway

public static IPAddress GetDefaultGateway()
{
    var card = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault();
    if(card == null) return null;
    var address = card.GetIPProperties().GatewayAddresses.FirstOrDefault();
    return address.Address;
}

edit (this you seeked):

var macAddr =
(
    from nic in NetworkInterface.GetAllNetworkInterfaces()
    where nic.OperationalStatus == OperationalStatus.Up
    select nic.GetPhysicalAddress().ToString()
).FirstOrDefault();
Community
  • 1
  • 1
fiinix
  • 142
  • 3
1

From a little more research there is no managed API that will let you do this. You need to either use the Windows API method described in How do I access ARP-protocol information through .NET? and http://www.pinvoke.net/default.aspx/iphlpapi.sendarp or you need to start a shell to run arp -a and capture the result.

Community
  • 1
  • 1
Brian O''Byrne
  • 550
  • 2
  • 10
  • the funny thing that "arp -a" gives me a different MAC address of the MAC address written on the back of my router.... the first 6 numbers are the same but the rest are differs ...... is it normal? – Yasser May 01 '13 at 00:07
  • Maybe. It depends on your router. I'd say it is likely that the MAC address printed on it is the address of the WAN interface, while your computer would be connected to one of the LAN interfaces. Obviously, each interface has a different MAC address. The structure of MAC addresses is such that the first three bytes are allocated to manufacturers and the remainder allocated by the manufacturer. Since every interface on your router has the same manufacturer it is normal for the MAC addresses to have the same first three bytes. – Brian O''Byrne May 01 '13 at 08:55