如何将结果写入csv?

我想对CSV中的主机名执行ping操作,并将结果写到下一列中,但是我有点不知道该怎么做?

    string filePath = @"c:\hostnames.csv";
    var reader = new StreamReader(filePath);
    Ping ping = new Ping();
    List<string> hostnames = new List<string>();
    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();
        var values = line.Split(',');
        hostnames.Add(values[0]);
        hostnames.ForEach(Console.WriteLine);
    }

    List<string> goodPing = new List<string>();
    foreach (string singleComputer in hostnames)
    {
        PingReply pingresult = ping.Send(singleComputer);

        if (pingresult.Status.ToString() == "Success")
        {
            goodPing.Add(singleComputer);
        }
    }
        var csv = new StringBuilder();
        var first = reader[0].ToString();   
        var newLine = string.Format("{0}");
        csv.AppendLine(newLine);
        File.WriteAllText(filePath, csv.ToString());
    }
评论