Humprey Evangelista Cogay

My Programming Adventures

June 2010 - Posts

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;
                }
            }