如何从 .txt 文件中读取服务器并使用 C# 应用程序对它们执行 ping 操作?
How to read from servers from .txt file and ping them with C# Application?
我有一个用 C# 编写的 Pinger 应用程序,我想从这个 .txt 文件 ping 所有服务器
servers.txt
这是我的代码:
{
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","whosebug.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();
}
}
您可以使用 File.ReadAllLines
读取文件中的所有文本并循环遍历行。
所以,这应该可以解决您的问题:
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);
}
}
}
}
我有一个用 C# 编写的 Pinger 应用程序,我想从这个 .txt 文件 ping 所有服务器
servers.txt
这是我的代码:
{
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","whosebug.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();
}
}
您可以使用 File.ReadAllLines
读取文件中的所有文本并循环遍历行。
所以,这应该可以解决您的问题:
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);
}
}
}
}