アドレスが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);
}
}
どんな助けでも大歓迎です。