Humprey Evangelista Cogay

My Programming Adventures

Get the workstations Physical Address (MAC Address)

I currently have a new project and as an additional security I want to get the workstations MAC address and store it in the Log..

Here's my solution which I based somewhere.....

            IPGlobalProperties NetworkProperties = IPGlobalProperties.GetIPGlobalProperties();
            NetworkInterface[] NetworkInterfaceCards= NetworkInterface.GetAllNetworkInterfaces();
            string macAddress = "";
            VeteransBankENT.ApplicationVariables.CurrentHostName = NetworkProperties.HostName;
            VeteransBankENT.ApplicationVariables.CurrentHostName = NetworkProperties.DomainName;

           if (NetworkInterfaceCards == null || NetworkInterfaceCards.Length < 1)
            {
                MessageBox.Show("  No network interfaces found.");
            }
           
            foreach (NetworkInterface adapter in NetworkInterfaceCards)
            {
                if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && adapter.OperationalStatus == OperationalStatus.Up)
                {
                    IPInterfaceProperties properties = adapter.GetIPProperties(); //  .GetIPInterfaceProperties();
                    PhysicalAddress address = adapter.GetPhysicalAddress();
                    byte[] bytes = address.GetAddressBytes();
                    for (int i = 0; i < bytes.Length; i++)
                    {
                         macAddress  += bytesIdea.ToString("X2");
                         if (i != bytes.Length - 1)
                        {
                            macAddress  +=  "-";
                        }
                    }
                    VeteransBankENT.ApplicationVariables.CurrentHost = macAddress;
                    break;
                }
            }

 

Comments

cvega said:

There's a quick hack in UuidCreateSequential:

[DllImport("rpcrt4.dll", SetLastError = true)]

       static extern int UuidCreateSequential(

                out Guid guid);

Guid g;

UuidCreateSequential(out g);

var b = g.ToByteArray();

// The last 7 bytes of UUID is the physical address:

Console.WriteLine("Physical Address: {0:x2}-{1:x2}-{2:x2}-{3:x2}-{4:x2}-{5:x2}", b[10], b[11], b[12], b[13], b[14], b[15]);

# June 3, 2010 10:04 PM

Comgen said:

Wow Thanks sir CVega... I will check on this =)....

# June 8, 2010 7:14 PM