简体   繁体   English

如何从服务器中读取 from.txt 文件并使用 C# 应用程序 ping 它们?

[英]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我有用 C# 编写的 Pinger 应用程序,我想从 this.txt 文件中 ping 所有服务器

servers.txt服务器.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.您可以使用File.ReadAllLines读取文件内的所有文本并循环遍历这些行。

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);
            }

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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