Ping 网站并将数据保存在 .csv 文件中

Ping a website and save the data in an .csv file

当代码为运行时,它必须每次ping 我指定的网站4 次,然后将结果写入.csv 文件。但我一直收到超时错误。谁能告诉我为什么?我尝试了很多不同的东西,到目前为止,注意是有效的。请帮帮我。

        static void Main(string[] args)
        {
            List<string> lstWebSites = new List<string>();
            lstWebSites.Add("www.yahoo.com");
            lstWebSites.Add("www.att.com");
            lstWebSites.Add("www.verizon");
            string filename = @"PingLog.csv";
            {
                using (var writer = new StreamWriter(filename, true)) 
                {
                    foreach(string website in lstWebSites)
                    {
                        writer.WriteLine(website);
                        try
                        {
                            Ping myPing = new Ping();
                            PingReply reply = myPing.Send(website, 1000);
                            if (reply != null)
                            {
                                Console.WriteLine("{0}, {1}", reply.Address, reply.RoundtripTime);
                            }
                        }                   
                        catch
                        {
                            Console.WriteLine.("ERROR: You have some TIMEOUT issue");
                        }
                    }
                }
            }
        }
    }
}

这是一个工作示例。我在您有语法错误或我对您的原始代码进行了调整的地方添加了一些评论。

// Missing quotes, should probably be a full file path
string filename = @"C:\temp\PingLog.csv";

// You had an extra opening brace here

// Open a file for writing using the filename, and a flag that means whether to append
using (var writer = new StreamWriter(filename, false))
{
    // Write a CSV header
    writer.WriteLine("Status, Time, Address");
    try
    {
        Ping myPing = new Ping();
        PingReply reply = myPing.Send("www.yahoo.com", 1000);
        if (reply != null)
        {
            // Use the overload of WriteLine that accepts string format and arguments
            writer.WriteLine("{0}, {1}, {2}", reply.Status, reply.RoundtripTime, reply.Address);
        }
    }
    catch
    {                   
        // You had a syntax error here
        Console.WriteLine("ERROR: You have some TIMEOUT issue");
    }
}

好的,我已经想通了大部分内容。非常感谢大家帮助我。 虽然,我仍然需要这个来 ping 至少三个网站,并为每个网站提供 4 个 ping 结果。 因此,如果有人可以请,请多帮我一点。 这是我所拥有的,到目前为止它有效:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace Ping Application
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = @"PingLog.csv";
            {
                using (var writer = new StreamWriter(filename, true)) 
                {
                    writer.WriteLine("www.yahoo.com", Time in MilliSeconds);
                    try
                    {
                        Ping myPing = new Ping();
                        PingReply reply = myPing.Send("www.yahoo.com", 1000);
                        if (reply != null)
                        {
                            Console.WriteLine("{0}, {1}, {2}", reply.Address, reply.RoundtripTime, reply.RoundtripTime);
                        }
                    }                   
                    catch
                    {
                        Console.WriteLine.("ERROR: You have some TIMEOUT issue");
                    }
                }
            }
        }
    }
}