16

In my C# application, I want to get my MAC address by using NetworkInterface class as the following:

NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()
{
    mac = nic.GetPhysicalAddress()
}

But this code returns the MAC without ':' or any other separator.

How can I retrieve the MAC in this format: 88:88:88:88:87:88 using the code above ONLY?

reformed
  • 4,505
  • 11
  • 62
  • 88
gln
  • 1,011
  • 5
  • 31
  • 61

5 Answers5

37

try

mac = string.Join (":", (from z in nic.GetPhysicalAddress().GetAddressBytes() select z.ToString ("X2")).ToArray());
Yahia
  • 69,653
  • 9
  • 115
  • 144
  • 10
    in .NET 4 it can be even shorter `mac = string.Join (":", nic.GetPhysicalAddress().GetAddressBytes().Select(b => b.ToString("X2")));` – Firo Feb 28 '13 at 08:08
  • 1
    Or: `mac = string.Join (":", Array.ConvertAll(nic.GetPhysicalAddress().GetAddressBytes(), b => b.ToString("X2")));` – dahall Mar 31 '20 at 18:00
3

The help for the command shows one way:

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.physicaladdress.aspx

    PhysicalAddress address = adapter.GetPhysicalAddress();
    byte[] bytes = address.GetAddressBytes();
    for(int i = 0; i< bytes.Length; i++)
    {
        // Display the physical address in hexadecimal.
        Console.Write("{0}", bytes[i].ToString("X2"));
        // Insert a hyphen after each byte, unless we are at the end of the 
        // address.
        if (i != bytes.Length -1)
        {
             Console.Write("-");
        }
    }
    Console.WriteLine();
Joe
  • 41,484
  • 20
  • 104
  • 125
  • 7
    This can be done even shorter: `BitConverter.ToString(byte[])` does exactly this formatting. – eFloh Sep 12 '11 at 06:59
  • @eFloh: Post that as an answer. – BoltClock Sep 12 '11 at 07:03
  • @BoltClock: This is already in the post linked below and only applies to this post, as it doesn't produce the requested output. See [this answer to Getting MAC Address C#](http://stackoverflow.com/questions/3157246/getting-mac-address-c/3157309#3157309) for an actual response. – eFloh Sep 12 '11 at 08:13
2

Using the comment by eFloh for using BitConverter I was able to do the following (assuming mac is predefined as a string).

foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
    mac = BitConverter.ToString(nic.GetPhysicalAddress().GetAddressBytes()).Replace('-', ':');

    //Do whatever else necessary with each mac...
}
Community
  • 1
  • 1
Jon Peterson
  • 723
  • 7
  • 21
1

Try Something like this:

// Insert Colons on MAC
string MACwithColons = "";
for (int i = 0; i < MAC.Length; i++) {
  MACwithColons = MACwithColons + MAC.Substring(i, 2) + ":";
  i++;
}
MACwithColons = MACwithColons.Substring(0, MACwithColons.Length - 1); // Remove the last colon
George Netu
  • 2,758
  • 4
  • 28
  • 49
Pabinator
  • 1,601
  • 1
  • 21
  • 25
1

Where you want to show that, you have to do this:

txtMac.text=getFormatMac(GetMacAddress());
public string GetMacAddress()

{
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    String sMacAddress = string.Empty;
    foreach (NetworkInterface adapter in nics)
    {
         if (sMacAddress == String.Empty)// solo retorna la mac de la primera tarjeta
         {
              IPInterfaceProperties properties = adapter.GetIPProperties();
              sMacAddress = adapter.GetPhysicalAddress().ToString();
          }
    }
    return sMacAddress;
}

public string getFormatMac(string sMacAddress)
{
    string MACwithColons = "";
    for (int i = 0; i < macName.Length; i++)
    {
        MACwithColons = MACwithColons + macName.Substring(i, 2) + ":";
        i++;
    }
    MACwithColons = MACwithColons.Substring(0, MACwithColons.Length - 1);

    return MACwithColons;
}

pableiros
  • 14,932
  • 12
  • 99
  • 105
H-JOSUE
  • 11
  • 1