34 lines
1014 B
C#
Executable File
34 lines
1014 B
C#
Executable File
using System.Net;
|
|
using System.Net.Sockets;
|
|
|
|
namespace Opsoft.NetCore.O3App.Shared
|
|
{
|
|
public class IpHelper
|
|
{
|
|
public static string GetIPv4()
|
|
{
|
|
string ip = string.Empty;
|
|
try
|
|
{
|
|
string hostName = Dns.GetHostName();
|
|
IPHostEntry IpEntry = Dns.GetHostEntry(hostName);
|
|
for (int i = 0; i < IpEntry.AddressList.Length; i++)
|
|
{
|
|
//从IP地址列表中筛选出IPv4类型的IP地址
|
|
//AddressFamily.InterNetwork表示此IP为IPv4,
|
|
//AddressFamily.InterNetworkV6表示此地址为IPv6类型
|
|
if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
|
|
{
|
|
ip = IpEntry.AddressList[i].ToString();
|
|
}
|
|
}
|
|
return ip;
|
|
}
|
|
catch
|
|
{
|
|
return "";
|
|
}
|
|
}
|
|
}
|
|
}
|