Ejemplo n.º 1
0
 private static bool NetworkInterfaceHasKeyword(NetworkInterface networkInterface, string keyword)
 {
     if (!HostUtility.ContactsIgnoreCase(networkInterface.Name, keyword))
     {
         return(HostUtility.ContactsIgnoreCase(networkInterface.Description, keyword));
     }
     return(true);
 }
Ejemplo n.º 2
0
 private static string GetIPAddressFromNetworkInterface()
 {
     try
     {
         NetworkInterface[]          networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
         Dictionary <int, IPAddress> source            = new Dictionary <int, IPAddress>();
         foreach (NetworkInterface networkInterface in networkInterfaces)
         {
             int key = 512;
             if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Loopback)
             {
                 key -= 256;
             }
             if (HostUtility.NetworkInterfaceHasKeyword(networkInterface, "Virtual"))
             {
                 key -= 128;
             }
             if (HostUtility.NetworkInterfaceHasKeyword(networkInterface, "本地连接") || HostUtility.NetworkInterfaceHasKeyword(networkInterface, "Local Area Connection"))
             {
                 key += 64;
             }
             if (networkInterface.OperationalStatus == OperationalStatus.Up)
             {
                 key += 32;
             }
             if (!source.ContainsKey(key))
             {
                 foreach (UnicastIPAddressInformation unicastAddress in networkInterface.GetIPProperties().UnicastAddresses)
                 {
                     if (unicastAddress.Address.AddressFamily == AddressFamily.InterNetwork)
                     {
                         source.Add(key, unicastAddress.Address);
                         break;
                     }
                 }
             }
         }
         return(source.OrderByDescending <KeyValuePair <int, IPAddress>, int>((Func <KeyValuePair <int, IPAddress>, int>)(item => item.Key)).FirstOrDefault <KeyValuePair <int, IPAddress> >().Value.ToString());
     }
     catch (Exception ex)
     {
         HostUtility._logger.Warn("GetIPAddressFromNetworkInterface Failed.", ex);
         return((string)null);
     }
 }
Ejemplo n.º 3
0
 static HostUtility()
 {
     HostUtility.Name = HostUtility.GetHostName();
     HostUtility.IPv4 = HostUtility.GetIPAddressFromNetworkInterface() ?? HostUtility.GetIPAddressFromDns();
 }