2015-10-19 18 views
1

我的代码工作正常,但是它将结果保存到.csv文件的位置我需要在其中进行一些更改。 我的结果是:在由逗号分隔的字符串行中打印所有4个乒乓

www.yahoo.com , 98.139.183.24 , 137 
www.att.com , 23.72.249.145 , 20 
www.yahoo.com , 98.139.183.24 , 120 
www.att.com , 23.72.249.145 , 16 

,我想我的结果是:

www.yahoo.com , 137 , 120 
www.att.com , 20 , 16 

在这个例子中,我分享只有两种结果,实际上,我回国4个结果,我需要把它们全部在一行中,我也需要摆脱IP地址。请帮帮我。然后

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

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     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.com"); 
      string filename = @"PingLog.csv"; 
      { 
       using (var writer = new StreamWriter(filename, true)) 
       { 
        for (int i = 0; i < 4; i++) 
        foreach(string website in lstWebSites) 
        { 
         //writer.WriteLine(website, lstWebSites); 
         try 
         { 
          Ping myPing = new Ping(); 
          PingReply reply = myPing.Send(website, 1000); 
          if (reply != null) 
          { 
           writer.WriteLine(website + " , " + reply.Address.ToString() + " , " + reply.RoundtripTime); 
          } 
         }     
         catch 
         { 
          Console.WriteLine("ERROR: You have some TIMEOUT issue"); 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+0

更好地使用字典>() – Steve

+0

我不明白你的意思吗? – NewDev

回答

0

如果你想显示你平安的所有调用的结果,而不是简List<string>保持站点的名称,你可以使用一个Dictionary<string, List<PingReply>>。通过这种方式,为向某网站每个查询你把所有收到的PingReply列表,并在循环结束时,你写一个文件中的一切(使用LINQ的一点帮助)

static void Main(string[] args) 
{ 
    // Dictionary to keep the sites list and the list of replies for each site 
    Dictionary<string, List<PingReply>> lstWebSites = new Dictionary<string, List<PingReply>>(); 
    lstWebSites.Add("www.yahoo.com", new List<PingReply>()); 
    lstWebSites.Add("www.att.com", new List<PingReply>()); 
    lstWebSites.Add("www.verizon.com", new List<PingReply>()); 
    string filename = @"d:\temp\PingLog.csv"; 

    // Start your loops 
    for (int i = 0; i < 4; i++) 
     foreach (string website in lstWebSites.Keys) 
     { 
      try 
      { 
       Ping myPing = new Ping(); 
       PingReply reply = myPing.Send(website, 1000); 
       if (reply != null) 
       { 
        // Do not write to file here, just add the 
        // the reply to your dictionary using the site key 
        lstWebSites[website].Add(reply); 
       } 
      } 
      catch 
      { 
       Console.WriteLine("ERROR: You have some TIMEOUT issue"); 
      } 
     } 


    using (var writer = new StreamWriter(filename, false)) 
    { 
     // For each site, extract the RoundtripTime and 
     // use string.Join to create a comma separated line to write 
     foreach(string website in lstWebSites.Keys) 
      writer.WriteLine(website + " , " + 
       string.Join(",", lstWebSites[website] 
         .Select(x => x.RoundtripTime) 
         .ToArray())); 
    } 

    string fileText = File.ReadAllText(filename); 
    Console.WriteLine(fileText); 

} 
+0

哇,我刚刚完成了所有的变化以及我想要的变化。非常感谢。这是一个非常大的帮助和完美。 – NewDev

0

最简单方法是在foreach循环内移动循环并收集每个站点的ping结果。您可以更改您的代码如下:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Net.NetworkInformation; 

namespace ConsoleApplication1 
{ 
    internal class Program 
    { 
     private static void Main(string[] args) 
     { 
      var lstWebSites = new List<string> 
      { 
       "www.yahoo.com", 
       "www.att.com", 
       "www.verizon.com" 
      }; 

      var filename = @"PingLog.csv"; 
      { 
       using (var writer = new StreamWriter(filename, true)) 
       { 
        foreach (var website in lstWebSites) 
        { 
         var roundTripTimes = new List<long>(); 

         for (var i = 0; i < 4; i++) 
         { 
          try 
          { 
           var myPing = new Ping(); 
           var reply = myPing.Send(website, 1000); 

           if (reply != null) 
           { 
            roundTripTimes.Add(reply.RoundtripTime); 
           } 
          } 
          catch 
          { 
           Console.WriteLine("ERROR: You have some TIMEOUT issue"); 
          } 
         } 

         writer.WriteLine("{0} , {1}", website, string.Join(" , ", roundTripTimes)); 
        } 
       } 
      } 
      Console.ReadKey(); 
     } 
    } 
} 

但它更好地学习一些LINQ(如LINQ 101),并以更简洁的方式是这样写代码:

using System; 
using System.Linq; 
using System.Collections.Generic; 
using System.IO; 
using System.Net.NetworkInformation; 

namespace ConsoleApplication1 
{ 
    internal class Program 
    { 
     private static void Main(string[] args) 
     { 
      var lstWebSites = new List<string> 
      { 
       "www.yahoo.com", 
       "www.att.com", 
       "www.verizon.com" 
      }; 

      var filename = @"PingLog.csv"; 

      var pings = Enumerable.Range(0, 4).SelectMany(_ => lstWebSites.Select(ws => new {Ping = new Ping(), WebSite = ws})); 
      var result = pings.Select(_ => new {Reply = _.Ping.Send(_.WebSite, 1000), _.WebSite}).ToLookup(_ => _.WebSite, p => p.Reply.RoundtripTime); 

      using (var writer = new StreamWriter(filename, true)) 
      { 
       foreach (var res in result) 
       { 
        writer.WriteLine("{0}, {1}", res.Key, string.Join(" , ", res)); 
       } 
      } 

      Console.ReadKey(); 
     } 
    } 
} 

此示例缺少异常处理但你可以理解主要想法。

+0

我会给这个尝试也...谢谢 – NewDev

+0

我得到一个红线在RoundtripTime下,我有if(result!= null)roundTripTimes.Add(result.RoundtripTime); – NewDev

+0

当您将鼠标指向此行时,您在工具提示中看到什么信息? –