我需要查找本地IP地址,因此我使用以下代码:
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}
return localIP;
我发现当一台PC有多个带有AddressFamily.InterNetwork
的IP时,我总是得到第一个。但是,我找不到任何属性来找出活动IP。
如何获得正确的IP?
感谢您的提示!
答案 0 :(得分:0)
我刚刚从互联网上选了这个。
string strHostName = System.Net.Dns.GetHostName(); ;
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
这应该为您提供一个包含您电脑的所有IP地址的数组。
答案 1 :(得分:0)
Bwall在this thread.
中发布了一个合适的解决方案foreach(NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
if(ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
Console.WriteLine(ni.Name);
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
Console.WriteLine(ip.Address.ToString());
}
}
}
}
这段代码中最重要的是,它只列出了以太网和无线接口的IP地址。我怀疑你的串行连接是否有效,所以这很可能无关紧要。另外,您始终可以编辑if语句。
<强> //修改 如果您只想要实际连接到互联网的IP地址,请使用Hosam Aly的solution
这是他的代码:
static IPAddress getInternetIPAddress()
{
try
{
IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
IPAddress gateway = IPAddress.Parse(getInternetGateway());
return findMatch(addresses, gateway);
}
catch (FormatException e) { return null; }
}
static string getInternetGateway()
{
using (Process tracert = new Process())
{
ProcessStartInfo startInfo = tracert.StartInfo;
startInfo.FileName = "tracert.exe";
startInfo.Arguments = "-h 1 www.example.com
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
tracert.Start();
using (StreamReader reader = tracert.StandardOutput)
{
string line = "";
for (int i = 0; i < 5; ++i)
line = reader.ReadLine();
line = line.Trim();
return line.Substring(line.LastIndexOf(' ') + 1);
}
}
}
static IPAddress findMatch(IPAddress[] addresses, IPAddress gateway)
{
byte[] gatewayBytes = gateway.GetAddressBytes();
foreach (IPAddress ip in addresses)
{
byte[] ipBytes = ip.GetAddressBytes();
if (ipBytes[0] == gatewayBytes[0]
&& ipBytes[1] == gatewayBytes[1]
&& ipBytes[2] == gatewayBytes[2])
{
return ip;
}
}
return null;
}
它基本上做的是跟踪到www.example.com的路线并从那里处理正确的IP。我在我的机器上测试了代码,需要将迭代次数从9改为5,以便从流中获得正确的行。你最好重新检查它,否则你可能会进入NullReferenceException,因为line
将是null
。
答案 2 :(得分:0)
static string GetActiveIP()
{
string ip = "";
foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces())
{
if (f.OperationalStatus == OperationalStatus.Up)
{
IPInterfaceProperties ipInterface = f.GetIPProperties();
if (ipInterface.GatewayAddresses.Count > 0)
{`enter code here`
foreach (UnicastIPAddressInformation unicastAddress in ipInterface.UnicastAddresses)
{
if ((unicastAddress.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) && (unicastAddress.IPv4Mask.ToString() != "0.0.0.0"))
{
ip = unicastAddress.Address.ToString();
break;
}
}`enter code here`
}
}
}
return ip;
}