c# - 如何将 ping 变量转换为 int?

标签 c# charts ping

我正在尝试将我的 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/

相关文章:

c# - 在 try catch 中抛出自定义异常

c# - 用另一个日期替换 DateTime 中的日期

django ping 谷歌 http 错误

c# - 如何使在我的类上使用自定义事件成为可选的?

css - 从 p :chart barchart 中删除网格和边框

javascript - 谷歌图表上的标签在特定配置中不可见

javascript - 在 Highcharts 中结合预处理数据和下钻

python - 无需 root 权限即可在 Python 中 Ping 服务器

java - JTextArea 的 Apache DefaultExecutor 和 Ping 命令

c# - 如何在 C# 中设置系统范围的 HotKey