C# 在控制台应用程序中获取 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/858341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get IP address in a console application
提问by
I am looking to figure out what my IP address is from a console application.
我想从控制台应用程序中找出我的 IP 地址。
I am used to a web application by using the Request.ServerVariables
collection and/or Request.UserHostAddress
.
我习惯于使用Request.ServerVariables
集合和/或Request.UserHostAddress
.
How can this be done in a console app?
这如何在控制台应用程序中完成?
采纳答案by CodeLikeBeaker
The easiest way to do this is as follows:
最简单的方法如下:
using System;
using System.Net;
namespace ConsoleTest
{
class Program
{
static void Main()
{
String strHostName = string.Empty;
// Getting Ip address of local machine...
// First get the host name of local machine.
strHostName = Dns.GetHostName();
Console.WriteLine("Local Machine's Host Name: " + strHostName);
// Then using host name, get the IP address list..
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
}
Console.ReadLine();
}
}
}
回答by Hannoun Yassir
using System;
using System.Net;
public class DNSUtility
{
public static int Main (string [] args)
{
String strHostName = new String ("");
if (args.Length == 0)
{
// Getting Ip address of local machine...
// First get the host name of local machine.
strHostName = DNS.GetHostName ();
Console.WriteLine ("Local Machine's Host Name: " + strHostName);
}
else
{
strHostName = args[0];
}
// Then using host name, get the IP address list..
IPHostEntry ipEntry = DNS.GetHostByName (strHostName);
IPAddress [] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
Console.WriteLine ("IP Address {0}: {1} ", i, addr[i].ToString ());
}
return 0;
}
}
回答by Nik
System.Net.Dns.GetHostAddresses() should do it.
System.Net.Dns.GetHostAddresses() 应该这样做。
回答by Martin Peck
The System.Net namespace is your friend here. In particular, APIs such as DNS.GetHostByName.
System.Net 命名空间是您的朋友。特别是诸如 DNS.GetHostByName 之类的 API。
However, any given machine may have multiple IP addresses (multiple NICs, IPv4 and IPv6 etc) so it's not quite as simple a question as you pose.
但是,任何给定的机器都可能有多个 IP 地址(多个 NIC、IPv4 和 IPv6 等),因此问题并不像您提出的那么简单。
回答by Prithis
Try this:
尝试这个:
String strHostName = Dns.GetHostName();
Console.WriteLine("Host Name: " + strHostName);
// Find host by name IPHostEntry
iphostentry = Dns.GetHostByName(strHostName);
// Enumerate IP addresses
int nIP = 0;
foreach(IPAddress ipaddress in iphostentry.AddressList) {
Console.WriteLine("IP #" + ++nIP + ": " + ipaddress.ToString());
}
回答by AZ_
IPAddress[] addresslist = Dns.GetHostAddresses(Dns.GetHostName());
IPAddress[] 地址列表 = Dns.GetHostAddresses(Dns.GetHostName());