-1

I have found this code to get a MAC address, but it returns a long string and doesn't include ':'.

Is it possible to add in the ':' or split up the string and add it it myself?

here is the code:

private object GetMACAddress()
{
    string macAddresses = "";

    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
    {
        if (nic.OperationalStatus == OperationalStatus.Up)
        {
            macAddresses += nic.GetPhysicalAddress().ToString();
            break;
        }
    }

    return macAddresses;
 }

It returns the value of 00E0EE00EE00 whereas I want it to display something like 00:E0:EE:00:EE:00.

Any ideas?

Thanks.

John H
  • 31
  • 5
  • Use a string builder, iterate from the end to the beginning and insert a `:` every two characters – Rafalon Aug 24 '18 at 10:34
  • 2
    It seems that it has already been answered here: https://stackoverflow.com/questions/7384211/formatting-mac-address-in-c-sharp – Dimitri Bouche Aug 24 '18 at 10:36

2 Answers2

4

I'm 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;
        }

Or You can use Join method, like this :

return string.Join (":", (from z in nic.GetPhysicalAddress().GetAddressBytes() select z.ToString ("X2")).ToArray());
Hossein
  • 3,083
  • 3
  • 16
  • 33
  • @JohnH, i would use the answer provided in the link of Dimitri Bouche (above) this would have a better performance comparing to ManagementObject but they both work so it is up to you. – Dimitri Aug 24 '18 at 10:40
1
using System;
using System.Text;

class Program
{
    static void Main()
    {
        Console.WriteLine(MACify("00E0EE00EE00"));
    }

    static string MACify(string input)
    {
        var builder = new StringBuilder(input);
        for(int i=builder.Length-2; i>0; i-=2)
        {
            builder.Insert(i,':');
        }

        return builder.ToString();
    }
}

Outputs:

00:E0:EE:00:EE:00

Rafalon
  • 4,450
  • 2
  • 16
  • 30