diff options
Diffstat (limited to 'Software/Visual_Studio/Tango.Transport/Helpers/IPAddressHelper.cs')
| -rw-r--r-- | Software/Visual_Studio/Tango.Transport/Helpers/IPAddressHelper.cs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Transport/Helpers/IPAddressHelper.cs b/Software/Visual_Studio/Tango.Transport/Helpers/IPAddressHelper.cs new file mode 100644 index 000000000..04a15caa3 --- /dev/null +++ b/Software/Visual_Studio/Tango.Transport/Helpers/IPAddressHelper.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Transport.Helpers +{ + public static class IPAddressHelper + { + public static string GetLocalIpAddress() + { + foreach (var netI in NetworkInterface.GetAllNetworkInterfaces()) + { + if (netI.NetworkInterfaceType != NetworkInterfaceType.Wireless80211 && + (netI.NetworkInterfaceType != NetworkInterfaceType.Ethernet || + netI.OperationalStatus != OperationalStatus.Up)) continue; + foreach (var uniIpAddrInfo in netI.GetIPProperties().UnicastAddresses.Where(x => netI.GetIPProperties().GatewayAddresses.Count > 0)) + { + + if (uniIpAddrInfo.Address.AddressFamily == AddressFamily.InterNetwork && + uniIpAddrInfo.AddressPreferredLifetime != uint.MaxValue) + return uniIpAddrInfo.Address.ToString(); + } + } + return null; + } + } +} |
