Ejemplo n.º 1
0
        public static string GetLocalIpAddress()
        {
            System.Net.NetworkInformation.UnicastIPAddressInformation mostSuitableIp = null;

            var networkInterfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();

            foreach (var network in networkInterfaces)
            {
                if (network.OperationalStatus != System.Net.NetworkInformation.OperationalStatus.Up)
                {
                    continue;
                }

                var properties = network.GetIPProperties();

                if (properties.GatewayAddresses.Count == 0)
                {
                    continue;
                }

                foreach (var address in properties.UnicastAddresses)
                {
                    if (address.Address.AddressFamily != AddressFamily.InterNetwork)
                    {
                        continue;
                    }

                    if (IPAddress.IsLoopback(address.Address))
                    {
                        continue;
                    }

                    if (!address.IsDnsEligible)
                    {
                        if (mostSuitableIp == null)
                        {
                            mostSuitableIp = address;
                        }
                        continue;
                    }

                    // The best IP is the IP got from DHCP server
                    if (address.PrefixOrigin != System.Net.NetworkInformation.PrefixOrigin.Dhcp)
                    {
                        if (mostSuitableIp == null || !mostSuitableIp.IsDnsEligible)
                        {
                            mostSuitableIp = address;
                        }
                        continue;
                    }

                    return(address.Address.ToString());
                }
            }

            return(mostSuitableIp != null
                ? mostSuitableIp.Address.ToString()
                : "");
        }
Ejemplo n.º 2
0
        //private System.Net.IPAddress GetIPAddress(string ComputerName, bool isReturnIPAddress)
        //{
        //    System.Net.IPAddress[] tempIP;
        //    string tmpBuilder = string.Empty;

        //    //tempIP = System.Net.Dns.Resolve( ComputerName ).AddressList;
        //    tempIP = System.Net.Dns.GetHostEntry( ComputerName ).AddressList;

        //    System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
        //    for(int i=0; i<tempIP.Length; i++)
        //    {
        //        strBuilder.Append(tempIP[i]);
        //    }

        //    tmpBuilder = strBuilder.ToString();
        //    tempIP = null;
        //    strBuilder = null;
        //    return System.Net.IPAddress.Parse(tmpBuilder);
        //}


        #region Get IPAddress new version
        private static System.Net.IPAddress CalculateNetwork(System.Net.NetworkInformation.UnicastIPAddressInformation addr)
        {
            // The mask will be null in some scenarios, like a dhcp address 169.254.x.x
            if (addr.IPv4Mask == null)
            {
                return(null);
            }

            var ip     = addr.Address.GetAddressBytes();
            var mask   = addr.IPv4Mask.GetAddressBytes();
            var result = new System.Byte[4];

            for (int i = 0; i < 4; ++i)
            {
                result[i] = (System.Byte)(ip[i] & mask[i]);
            }

            return(new System.Net.IPAddress(result));
        }
Ejemplo n.º 3
0
        public static string GuessLocalIpAddress(System.Net.NetworkInformation.NetworkInterfaceType?ofType)
        {
            System.Net.NetworkInformation.UnicastIPAddressInformation mostSuitableIp    = null;
            System.Net.NetworkInformation.NetworkInterface[]          networkInterfaces =
                System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();

            foreach (System.Net.NetworkInformation.NetworkInterface network in networkInterfaces)
            {
                if (ofType.HasValue && network.NetworkInterfaceType != ofType)
                {
                    continue;
                }

                if (network.OperationalStatus != System.Net.NetworkInformation.OperationalStatus.Up)
                {
                    continue;
                }

                if (network.Description.ToLower().Contains("virtual") ||
                    network.Description.ToLower().Contains("pseudo")
                    )
                {
                    continue;
                }

                System.Net.NetworkInformation.IPInterfaceProperties properties = network.GetIPProperties();

                if (properties.GatewayAddresses.Count == 0)
                {
                    continue;
                }

                foreach (System.Net.NetworkInformation.UnicastIPAddressInformation address in properties.UnicastAddresses)
                {
                    if (address.Address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        continue;
                    }

                    if (System.Net.IPAddress.IsLoopback(address.Address))
                    {
                        continue;
                    }

                    if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows) &&
                        !address.IsDnsEligible)
                    {
                        if (mostSuitableIp == null)
                        {
                            mostSuitableIp = address;
                        }
                        continue;
                    }

                    // The best IP is the IP got from DHCP server
                    if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows) &&
                        address.PrefixOrigin != System.Net.NetworkInformation.PrefixOrigin.Dhcp)
                    {
                        if (mostSuitableIp == null || !mostSuitableIp.IsDnsEligible)
                        {
                            mostSuitableIp = address;
                        }
                        continue;
                    }

                    // System.Console.WriteLine(address.IPv4Mask); // Subnet Mask
                    return(address.Address.ToString());
                }
            }

            return(mostSuitableIp != null
                ? mostSuitableIp.Address.ToString()
                : "");
        }