我正在尝试将我的 PC 的 ping 处理成图表,这几乎成功了,但是我的 ping 变量不想转换为 int
我在互联网上尝试了很多视频和网站,但我没有成功!我制作了一个 ping 变量,然后尝试将其转换为一个 int,这样我就可以将它放入图表中,但它无法转换!
这是我的代码:
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Threading;
using System.Windows.Forms.DataVisualization.Charting;
private void pingTimer_Tick(object sender, EventArgs e)
{
Ping ping1 = new Ping();
PingReply PR = ping1.Send("192.168.2.6");
Convert.ToInt32(PR);
chart1.Series["Connection"].Points.AddY(PR);
}
我没有编译器错误,但是当我启动计时器时,它崩溃并提示
Cannot convert a System.Net.NetworkInformation.PingReply object to the System.IConvertible type."
甚至可以将其转换为 int 吗?
最佳答案
使用 RoundtripTime
以毫秒为单位保存往返(发送 + 接收)时间的属性。
Gets the number of milliseconds taken to send an Internet Control Message Protocol (ICMP) echo request and receive the corresponding ICMP echo reply message.
int milliseconds = (int)PR.RoundtripTime;
所以你的代码应该是这样的
chart1.Series["Connection"].Points.AddY((int)PR.RoundtripTime);
关于c# - 如何将 ping 变量转换为 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56168947/