可能重复:
How to get my own IP address in C#?
我需要获得系统的IP地址,而应用程序是通过C#代码运行的。
IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress theaddress in ip)
{
String _ipAddress = theaddress.ToString();
}我正在使用这段代码,但这在不同的操作系统中给出了不同的结果。例如,在Windows 7中,它给出了“fe80::E3:148 d:6e5b:bcaa%14”
而Windows则给出了"192.168.10.93“。
发布于 2010-08-25 18:19:29
请注意,您可能有分配给计算机的多个IP地址。您可以这样检索它们(注意:这段代码忽略了回送地址):
var iplist = new List<string>();
foreach (var iface in NetworkInterface.GetAllNetworkInterfaces())
{
var ips = iface.GetIPProperties().UnicastAddresses;
foreach (var ip in ips)
if (ip.Address.AddressFamily == AddressFamily.InterNetwork &&
ip.Address.ToString() != "127.0.0.1")
iplist.Add(ip.Address.ToString());
}所使用的命名空间包括:
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;发布于 2010-08-25 18:18:15
给你-快谷歌:
http://msdn.microsoft.com/en-us/library/system.net.ipaddress(v=VS.100).aspx
https://stackoverflow.com/questions/3568959
复制相似问题