C# 为什么可以';在c中打印NSC查找中的所有ip地址#

C# 为什么可以';在c中打印NSC查找中的所有ip地址#,c#,winforms,ip,nslookup,C#,Winforms,Ip,Nslookup,您好,我有一个问题,我正在尝试获取nslookup域的所有ip地址。我在一个按钮上使用了下面的c#脚本,但它只打印出1个ip地址,我做错了什么 string myHost = "domain.com"; string myIP = null; for (int i = 0; i <= System.Net.Dns.GetHostEntry(myHost).AddressList.Length - 1; i++) { if (System.Net.Dns.GetHostEntry

您好,我有一个问题,我正在尝试获取nslookup域的所有ip地址。我在一个按钮上使用了下面的c#脚本,但它只打印出1个ip地址,我做错了什么

string myHost = "domain.com";
string myIP = null;


for (int i = 0; i <= System.Net.Dns.GetHostEntry(myHost).AddressList.Length - 1; i++)
{
    if (System.Net.Dns.GetHostEntry(myHost).AddressList[i].IsIPv6LinkLocal == false)
    {
        //myIP = System.Net.Dns.GetHostEntry(myHost).AddressList[i].ToString();
        txtIp.Text = System.Net.Dns.GetHostEntry(myHost).AddressList[i].ToString();
    }
}
string myHost=“domain.com”;
字符串myIP=null;

对于(int i=0;i首先,您应该避免发出dns请求3次。将结果存储在变量中

其次,将
txtIp.Text
设置为最后一个条目。您需要附加字符串,但需要替换它们。请尝试以下代码:

string myHost = "domain.com";
string myIP = null;
IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(myHost);

for (int i = 0; i <= hostEntry.AddressList.Length - 1; i++)
{
    if (!hostEntry.AddressList[i].IsIPv6LinkLocal)
    {
        txtIp.Text += hostEntry.AddressList[i].ToString();
    }
}

这将为您提供一个以逗号分隔的ip地址列表。

非常感谢,我终于让它按我想要的方式工作了;)
string myHost = "domain.com";
string myIP = null;
IPHostEntry hostEntry = System.Net.Dns.GetHostEntry(myHost);
txtIP.Text = string.Join(", ", hostEntry.AddressList.Where(ip => !ip.IsIPv6LinkLocal).Select(ip => ip.ToString()));