gpt4 book ai didi

c# - 如何获取您的 PING 并将其贴在标签上

转载 作者:太空狗 更新时间:2023-10-30 00:15:26 25 4
gpt4 key购买 nike

我试图找到如何获取我的 ping 并找到了这段代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
using System.IO;
using System.Configuration;
using System.Web;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
Ping ping = new Ping();
PingReply pingreply = ping.Send("www.google.com");
StreamWriter sw = new StreamWriter(@"C:\ping.txt");
StringBuilder sb = new StringBuilder();
sb.Append("Address: " + pingreply.Address + "\r\n");
sb.Append("Roundtrip Time: " + pingreply.RoundtripTime + "\r\n");
sb.Append("TTL (Time To Live): " + pingreply.Options.Ttl + "\r\n");
sb.Append("Buffer Size: " + pingreply.Buffer.Length.ToString() + "\r\n");
sb.Append("Time : " + DateTime.Now + "\r\n");
sw.Write(sb.ToString());
sw.Close();
}
catch (Exception err)
{
}
}
}
}

我试着做这样的事情:

label1.Text = pingreply.Buffer.Length.ToString();

但是ping不通,我不知道如何把它贴在标签上,有人知道怎么做吗?

最佳答案

您正在捕获所有错误并忽略它们.. 可能是由于您试图将其写入文件造成的。

只需这样做:

label1.Text = new Ping().Send("www.google.com").RoundtripTime.ToString() + "ms";

或者,妥善处理(感谢@Ichabod Clay)

using (Ping p = new Ping()) {
label1.Text = p.Send("www.google.com").RoundtripTime.ToString() + "ms";
}

关于c# - 如何获取您的 PING 并将其贴在标签上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14616306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com