12345678910111213141516171819202122232425262728293031323334353637 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- namespace Aitex.Core.Utilities
- {
- public static class Network
- {
- const string IpPrefix = "192.18.";
- static string _localIP;
- public static string LocalIP
- {
- get
- {
- if (string.IsNullOrWhiteSpace(_localIP))
- {
- string hostName = Dns.GetHostName();
- IPHostEntry me = Dns.GetHostEntry(hostName);
- IPAddress[] ips = me.AddressList;
- Func<IPAddress, bool> predicateIPV4 = ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork;
- Func<IPAddress, bool> predicate192 = ip => predicateIPV4(ip) && ip.ToString().StartsWith(IpPrefix);
- _localIP = ips.Any(predicate192) ? ips.First(predicate192).ToString()
- : ips.Any(predicateIPV4) ? ips.First(predicateIPV4).ToString()
- : (ips.Length > 0 ? ips[0]: new IPAddress(0x0)).ToString();
- }
- return _localIP;
- }
- }
- }
- }
|