我在尝试确定地址是 IP 地址还是主机名时遇到问题。我发现的所有内容都说使用正则表达式。我不确定如何形成 IF 语句。这是我的代码:
private void btnPingAddress_Click(object sender, EventArgs e)
{
intByteSize = Convert.ToInt32(numericDataSize.Value);
intNumberOfPings = Convert.ToInt32(numericPing.Value);
strDnsAddress = cmbPingAddress.Text;
//If address is IP address:
if (strDnsAddress Contains ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?")
{
txtPingResults.Text = "Pinging " + strIpAddress + " with " + intByteSize + " bytes of data:" + "\r\n";
}
// If address is hostname:
else
{
strIpAddress = Convert.ToString(Dns.GetHostEntry(strDnsAddress));
txtPingResults.Text = "Pinging " + strDnsAddress + " [" + strIpAddress + "] with " + intByteSize + " bytes of data:" + "\r\n";
}
Ping ping = new Ping();
PingReply reply = ping.Send(cmbPingAddress.Text);
txtPingResults.Text = "Pinging " + cmbPingAddress.Text + " [" + Convert.ToString(reply.Address) + "] with " + intByteSize + " bytes of data:" + "\r\n";
for (int i = 0; i < intNumberOfPings; i++)
{
txtPingResults.AppendText("Reply from "+Convert.ToString(reply.Address)+": Bytes="+Convert.ToString(intByteSize) +" Time="+Convert.ToString(reply.RoundtripTime) +"ms"+" TTL="+Convert.ToString(reply.Options.Ttl)+ "\r\n");
cmbPingAddress.Items.Add(cmbPingAddress.Text);
}
}
任何帮助将不胜感激。