顶部信息栏,显示网站标题、Logo等资

Get MAC Addres

1/24/2015 Source:Backyard [comment]


        /// 
        /// By DOS to get MAC address
        /// 
        /// 
        public string GetMacAddressByDos()
        {
            string macAddress = "";
            Process p = null;
            StreamReader reader = null;
            try
            {
                ProcessStartInfo start = new ProcessStartInfo("cmd.exe"); 

                start.FileName = "ipconfig";
                start.Arguments = "/all"; 

                start.CreateNoWindow = true; 

                start.RedirectStandardOutput = true; 

                start.RedirectStandardInput = true; 

                start.UseShellExecute = false; 

                p = Process.Start(start);

                reader = p.StandardOutput; 

                string line = reader.ReadLine(); 

                while (!reader.EndOfStream)
                {
                    if (line.ToLower().IndexOf("physical address") > 0 || line.ToLower().IndexOf("physical address") > 0)
                    {
                        int index = line.IndexOf(":");
                        index += 2;
                        macAddress = line.Substring(index);
                        macAddress = macAddress.Replace('-', ':');
                        break;
                    }
                    line = reader.ReadLine();
                }
            }
            catch
            {

            }
            finally
            {
                if (p != null)
                {
                    p.WaitForExit(); 
                    p.Close(); 
                }
                if (reader != null)
                {
                    reader.Close(); 
                }
            }
            return macAddress;
        } 
        [DllImport("Iphlpapi.dll")]
        public static extern uint GetAdaptersAddresses(uint Family, uint flags, IntPtr Reserved,
            IntPtr PAdaptersAddresses, ref uint pOutBufLen);

        /// 
        ///  MAC
        /// 
        /// 
        public string GetMacAddressByAdapter()
        {
            string macAddress = "";
            try
            {
                IntPtr PAdaptersAddresses = new IntPtr();

                uint pOutLen = 100;
                PAdaptersAddresses = Marshal.AllocHGlobal(100);

                uint ret =
                    GetAdaptersAddresses(0, 0, (IntPtr)0, PAdaptersAddresses, ref pOutLen);

                if (ret == 111)
                {
                    Marshal.FreeHGlobal(PAdaptersAddresses);
                    PAdaptersAddresses = Marshal.AllocHGlobal((int)pOutLen);
                    ret = GetAdaptersAddresses(0, 0, (IntPtr)0, PAdaptersAddresses, ref pOutLen);
                }

                IP_Adapter_Addresses adds = new IP_Adapter_Addresses();

                IntPtr pTemp = PAdaptersAddresses;

                while (pTemp != (IntPtr)0)
                {
                    Marshal.PtrToStructure(pTemp, adds);
                    string adapterName = Marshal.PtrToStringAnsi(adds.AdapterName);
                    string FriendlyName = Marshal.PtrToStringAuto(adds.FriendlyName);
                    string tmpString = string.Empty;

                    for (int i = 0; i < 6; i++)
                    {
                        tmpString += string.Format("{0:X2}", adds.PhysicalAddress[i]);

                        if (i < 5)
                        {
                            tmpString += ":";
                        }
                    }


                    RegistryKey theLocalMachine = Registry.LocalMachine;

                    RegistryKey theSystem
                        = theLocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces");
                    RegistryKey theInterfaceKey = theSystem.OpenSubKey(adapterName);

                    if (theInterfaceKey != null)
                    {
                        macAddress = tmpString;
                        break;
                    }

                    pTemp = adds.Next;
                }
            }
            catch 
            { }
            return macAddress;
        }
        /// 
        /// By NetBios to get MAC
        /// 
        /// 
        public string GetMacAddressByNetBios()
        {
            string macAddress = "";
            try
            {
                string addr = "";
                int cb;
                ASTAT adapter;
                NCB Ncb = new NCB();
                char uRetCode;
                LANA_ENUM lenum;

                Ncb.ncb_command = (byte)NCBCONST.NCBENUM;
                cb = Marshal.SizeOf(typeof(LANA_ENUM));
                Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
                Ncb.ncb_length = (ushort)cb;
                uRetCode = Win32API.Netbios(ref   Ncb);
                lenum = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM));
                Marshal.FreeHGlobal(Ncb.ncb_buffer);
                if (uRetCode != (short)NCBCONST.NRC_GOODRET)
                    return "";

                for (int i = 0; i < lenum.length; i++)
                {
                    Ncb.ncb_command = (byte)NCBCONST.NCBRESET;
                    Ncb.ncb_lana_num = lenum.lana[i];
                    uRetCode = Win32API.Netbios(ref   Ncb);
                    if (uRetCode != (short)NCBCONST.NRC_GOODRET)
                        return "";

                    Ncb.ncb_command = (byte)NCBCONST.NCBASTAT;
                    Ncb.ncb_lana_num = lenum.lana[i];
                    Ncb.ncb_callname[0] = (byte)'*';
                    cb = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER)) * (int)NCBCONST.NUM_NAMEBUF;
                    Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
                    Ncb.ncb_length = (ushort)cb;
                    uRetCode = Win32API.Netbios(ref   Ncb);
                    adapter.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS));
                    Marshal.FreeHGlobal(Ncb.ncb_buffer);

                    if (uRetCode == (short)NCBCONST.NRC_GOODRET)
                    {
                        if (i > 0)
                            addr += ":";
                        addr = string.Format("{0,2:X}:{1,2:X}:{2,2:X}:{3,2:X}:{4,2:X}:{5,2:X}",
                              adapter.adapt.adapter_address[0],
                              adapter.adapt.adapter_address[1],
                              adapter.adapt.adapter_address[2],
                              adapter.adapt.adapter_address[3],
                              adapter.adapt.adapter_address[4],
                              adapter.adapt.adapter_address[5]);
                    }
                }
                macAddress = addr.Replace(' ', '0');

            }
            catch
            {
            }
            return macAddress;
        }