简体   繁体   中英

How to read from servers from .txt file and ping them with C# Application?

I have Pinger Application written in C# and I want to ping all servers from this.txt file

servers.txt

Here is my code:

 {
     private static void Main(string[] args)
     {
         if (args == null) throw new ArgumentNullException("args");
         Console.WriteLine("Pinger");

         var waiter = new ManualResetEventSlim(false);

         var pingData = new MultiPing(new[] { "google.com","bing.com","stackoverflow.com" }, waiter, 300);

         waiter.Wait();

         Console.WriteLine("Pings:");
         Console.WriteLine(pingData.GetPingInformation());

         Console.WriteLine("Server with lowest ping latency:");
         Console.WriteLine(pingData.GetIp());

         Console.ReadLine();
     }
 }

You can use File.ReadAllLines to read all the text inside a file and loop through the lines.

So, this should solve your problem:

using System;
using System.IO;
using System.Threading;

namespace SO.DtProblem
{
    class Program
    {
        static void Main(string[] args)
        { 
            var servers = File.ReadAllLines(@"C:\temp\servers.txt");

            foreach (var line in servers)
            {
                string currentServer = line;
                //Your ping code per currentServer should be here
                //Or do somthing else as you wish

                Console.WriteLine(line);
            }

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM