我刚刚找到了一个ping应用程序的代码,它可以ping ip地址和域名,但当我将它集成到一个父窗体中时,它不能ping ip地址,但它确实能ping域名。有人能为此提供解决方案吗?代码如下所示。
public FrmPing()
{
InitializeComponent();
}
private void btnPing_Click(object sender, EventArgs e)
{
try
{
int c = 3;
IPAddress ipAddress = Dns.GetHostEntry(hostTextBox.Text).AddressList[0];
resultsListView.Items .Clear();
for (int i = 0; i < c; i++)
{
System.Net.NetworkInformation.Ping ping =
new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply pingReply =
ping.Send(ipAddress);
ListViewItem result = new ListViewItem(pingReply.Address.ToString());
result.SubItems.Add(pingReply.Buffer.Count().ToString());
result.SubItems.Add(pingReply.RoundtripTime.ToString());
result.SubItems.Add(pingReply.Options.Ttl.ToString());
result.SubItems.Add(pingReply.Status.ToString());
resultsListView.Items.Add(result);
System.Threading.Thread.Sleep(100);
}
}
catch (SocketException)
{
MessageBox.Show("Could not resolve host name.");
}
catch (PingException ex)
{
MessageBox.Show(ex.Message);
}
catch (ArgumentNullException)
{
MessageBox.Show("Please enter the host name or IP address to ping.");
}
hostTextBox.Focus();
}发布于 2014-04-09 16:39:23
如果您想ping一个地址,而不是域名,您可以更改
IPAddress ipAddress = Dns.GetHostEntry(hostTextBox.Text).AddressList[0];使用
IPAddress ipAddress = IPAddress.Parse("youradress");https://stackoverflow.com/questions/22952031
复制相似问题