示例#1
0
        public virtual List <string> GetMacAddresses()
        {
            List <string> macAddresses = new List <string>();

            if (Environment.OSVersion.Platform == PlatformID.Win32NT ||
                Environment.OSVersion.Platform == PlatformID.Win32Windows)
            {
                using (var query = new ManagementObjectSearcher("Select MacAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE"))
                    using (var mgmntObjects = query.Get())
                    {
                        foreach (ManagementObject mo in mgmntObjects)
                        {
                            string mac = (string)mo["MacAddress"];
                            if (!String.IsNullOrEmpty(mac) &&
                                mac != "00:00:00:00:00:00")
                            {
                                mac = mac.Replace(":", "");
                                if (!macAddresses.Contains(mac))
                                {
                                    macAddresses.Add(mac);
                                }
                            }
                            mo.Dispose();
                        }
                    }
            }
            else
            {
                System.Net.NetworkInformation.NetworkInterfaceType Type = 0;
                try
                {
                    System.Net.NetworkInformation.NetworkInterface[] theNetworkInterfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
                    foreach (System.Net.NetworkInformation.NetworkInterface currentInterface in theNetworkInterfaces)
                    {
                        Type = currentInterface.NetworkInterfaceType;
                        if (Type == System.Net.NetworkInformation.NetworkInterfaceType.Ethernet || Type == System.Net.NetworkInformation.NetworkInterfaceType.GigabitEthernet || Type == System.Net.NetworkInformation.NetworkInterfaceType.FastEthernetFx)
                        {
                            string mac = currentInterface.GetPhysicalAddress().ToString();
                            mac = mac.Replace(":", "");
                            if (!macAddresses.Contains(mac))
                            {
                                macAddresses.Add(mac);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                }
            }

            return(macAddresses);
        }
        internal SystemNetworkInterface(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo)
        {
            this.id              = ipAdapterInfo.adapterName;
            this.name            = string.Empty;
            this.description     = ipAdapterInfo.description;
            this.index           = ipAdapterInfo.index;
            this.physicalAddress = ipAdapterInfo.address;
            this.addressLength   = ipAdapterInfo.addressLength;
            if (ComNetOS.IsWin2K && !ComNetOS.IsPostWin2K)
            {
                this.name = this.ReadAdapterName(this.id);
            }
            if (this.name.Length == 0)
            {
                this.name = this.description;
            }
            SystemIPv4InterfaceStatistics statistics = new SystemIPv4InterfaceStatistics((long)this.index);

            this.operStatus = statistics.OperationalStatus;
            switch (ipAdapterInfo.type)
            {
            case OldInterfaceType.Ppp:
                this.type = System.Net.NetworkInformation.NetworkInterfaceType.Ppp;
                break;

            case OldInterfaceType.Loopback:
                this.type = System.Net.NetworkInformation.NetworkInterfaceType.Loopback;
                break;

            case OldInterfaceType.Slip:
                this.type = System.Net.NetworkInformation.NetworkInterfaceType.Slip;
                break;

            case OldInterfaceType.Fddi:
                this.type = System.Net.NetworkInformation.NetworkInterfaceType.Fddi;
                break;

            case OldInterfaceType.Ethernet:
                this.type = System.Net.NetworkInformation.NetworkInterfaceType.Ethernet;
                break;

            case OldInterfaceType.TokenRing:
                this.type = System.Net.NetworkInformation.NetworkInterfaceType.TokenRing;
                break;

            default:
                this.type = System.Net.NetworkInformation.NetworkInterfaceType.Unknown;
                break;
            }
            this.interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterInfo);
        }
 internal SystemNetworkInterface(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses)
 {
     this.id                  = ipAdapterAddresses.AdapterName;
     this.name                = ipAdapterAddresses.friendlyName;
     this.description         = ipAdapterAddresses.description;
     this.index               = ipAdapterAddresses.index;
     this.physicalAddress     = ipAdapterAddresses.address;
     this.addressLength       = ipAdapterAddresses.addressLength;
     this.type                = ipAdapterAddresses.type;
     this.operStatus          = ipAdapterAddresses.operStatus;
     this.ipv6Index           = ipAdapterAddresses.ipv6Index;
     this.adapterFlags        = ipAdapterAddresses.flags;
     this.interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterAddresses);
 }
        internal SystemNetworkInterface(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo)
        {
            this.id = ipAdapterInfo.adapterName;
            this.name = string.Empty;
            this.description = ipAdapterInfo.description;
            this.index = ipAdapterInfo.index;
            this.physicalAddress = ipAdapterInfo.address;
            this.addressLength = ipAdapterInfo.addressLength;
            if (ComNetOS.IsWin2K && !ComNetOS.IsPostWin2K)
            {
                this.name = this.ReadAdapterName(this.id);
            }
            if (this.name.Length == 0)
            {
                this.name = this.description;
            }
            SystemIPv4InterfaceStatistics statistics = new SystemIPv4InterfaceStatistics((long) this.index);
            this.operStatus = statistics.OperationalStatus;
            switch (ipAdapterInfo.type)
            {
                case OldInterfaceType.Ppp:
                    this.type = System.Net.NetworkInformation.NetworkInterfaceType.Ppp;
                    break;

                case OldInterfaceType.Loopback:
                    this.type = System.Net.NetworkInformation.NetworkInterfaceType.Loopback;
                    break;

                case OldInterfaceType.Slip:
                    this.type = System.Net.NetworkInformation.NetworkInterfaceType.Slip;
                    break;

                case OldInterfaceType.Fddi:
                    this.type = System.Net.NetworkInformation.NetworkInterfaceType.Fddi;
                    break;

                case OldInterfaceType.Ethernet:
                    this.type = System.Net.NetworkInformation.NetworkInterfaceType.Ethernet;
                    break;

                case OldInterfaceType.TokenRing:
                    this.type = System.Net.NetworkInformation.NetworkInterfaceType.TokenRing;
                    break;

                default:
                    this.type = System.Net.NetworkInformation.NetworkInterfaceType.Unknown;
                    break;
            }
            this.interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterInfo);
        }
 internal SystemNetworkInterface(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses)
 {
     this.id = ipAdapterAddresses.AdapterName;
     this.name = ipAdapterAddresses.friendlyName;
     this.description = ipAdapterAddresses.description;
     this.index = ipAdapterAddresses.index;
     this.physicalAddress = ipAdapterAddresses.address;
     this.addressLength = ipAdapterAddresses.addressLength;
     this.type = ipAdapterAddresses.type;
     this.operStatus = ipAdapterAddresses.operStatus;
     this.ipv6Index = ipAdapterAddresses.ipv6Index;
     this.adapterFlags = ipAdapterAddresses.flags;
     this.interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterAddresses);
 }
示例#6
0
        public static string GetLocalIPv4(System.Net.NetworkInformation.NetworkInterfaceType _type)
        {
            System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
            var ipgp = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();

            string output = "";

            foreach (System.Net.NetworkInformation.NetworkInterface item in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())
            {
                var interfaceProperties = item.GetIPProperties();

                // if (!string.Equals(interfaceProperties.DnsSuffix, ipgp.DomainName, System.StringComparison.InvariantCultureIgnoreCase))
                // continue;

                if (interfaceProperties.DnsAddresses == null || interfaceProperties.DnsAddresses.Count < 1)
                {
                    continue;
                }

                if (interfaceProperties.DnsAddresses[0].AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    continue;
                }

                if (item.NetworkInterfaceType == _type && item.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up)
                {
                    foreach (System.Net.NetworkInformation.UnicastIPAddressInformation ip in interfaceProperties.UnicastAddresses)
                    {
                        if (!ip.IsDnsEligible)
                        {
                            continue;
                        }

                        if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            output = ip.Address.ToString();
                        }
                    }
                }
            }
            return(output);
        }
示例#7
0
    static public string MACAddress()
    {
#if false
        // !UNITY_STANDALONE_WIN
        System.Net.NetworkInformation.NetworkInterfaceType Type = 0;

        string MacAddress = "AA:AA:AA:AA:AA:AA";        //ModSupBase.EMPTY_STRING;

        try
        {
            System.Net.NetworkInformation.NetworkInterface[] theNetworkInterfaces =
                System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();

            foreach (System.Net.NetworkInformation.NetworkInterface currentInterface in theNetworkInterfaces)
            {
                Type = currentInterface.NetworkInterfaceType;

                if (Type == System.Net.NetworkInformation.NetworkInterfaceType.Ethernet ||
                    Type == System.Net.NetworkInformation.NetworkInterfaceType.GigabitEthernet ||
                    Type == System.Net.NetworkInformation.NetworkInterfaceType.FastEthernetFx)
                {
                    MacAddress = currentInterface.GetPhysicalAddress().ToString();
                    break;
                }
            }
        }
        catch (System.Exception ex)
        {
            //    ModErrorHandle.Error_Handler(ex);
            Debug.LogError("obtaining MAC address failed:" + ex.Message);
            //ModSupBase.EMPTY_STRING;
        }
        return(MacAddress);
#else
        return("AA:AA:AA:AA:AA:AA");
#endif
    }