Monday, June 28, 2010

Determine Physical Address (MAC address)

In today's post I want to show, how to readout the MAC address or physical address of the computer in C#.
The MAC address is the physical hardware address of the network adapter, by which this can be uniquely identified all over the world as it is only given once (theoretically though, in fact it is the cased that it gets reused and can also easily be faked).
So if your computer has a LAN card and connects to the internet using this, it can be identified by the MAC address of this.
With C# the MAC address can either be determined with the help of the class System.Management.ManagementClass, but the code for this is quite complicated.
There is a much more easier call to get the same result.
The following code is a C# console application, which iterates over all network adapters of the computer and outputs the corresponding description (name) and MAC address (using System.Net.NetworkInformation; has to be included):

            NetworkInterface[] NetworkAdapters = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface adapter in NetworkAdapters)
                Console.WriteLine("MAC Address: " + adapter.GetPhysicalAddress().ToString());

No comments:

Post a Comment