如何将结果写入 csv?
How can I write the result to the csv?
我想对 csv 中的主机名执行 ping 操作,并将结果写在下一列中,但我有点不知所措如何操作?
这是错误 I get:Error CS0021 无法将带有 [] 的索引应用于 'StreamReader'
类型的表达式
我唯一能做的就是将 csv 中的内容写入控制台。
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());
}
不清楚您得到的错误是什么,但我注意到一个问题是您没有向 string.Format 提供变量,我认为它在那里没用..
示例 hostnames.csv 我假设是这样的:
www.google.com,www.somebadu-rlwhichcannot..com,www.whosebug.com
var filePath = @"c:\test\hostnames.csv"; // change it to your source
var filePathOutput = @"c:\test\hostnamesOutput.csv"; // use separate output file so you would not overwrite your source..
var ping = new Ping();
var hostNames = new List<string>();
using (var reader = new StreamReader(filePath))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
hostNames = line.Split(',').ToList();
}
}
var goodPing = new List<string>();
foreach (var singleComputer in hostNames)
try
{
var pingResult = ping.Send(singleComputer);
if (pingResult.Status == IPStatus.Success)
{
goodPing.Add(singleComputer);
}
}
catch (Exception e)
{
// host was not in a correct format or any other exception thrown....
// do whatever error handling you want, logging etc...
}
var csv = new StringBuilder();
foreach (var hostname in hostNames)
{
var resultText = goodPing.Contains(hostname) ? "Success" : "Failed";
var newLine = string.Format("{0},{1}", hostname, resultText);
csv.AppendLine(newLine);
}
File.WriteAllText(filePathOutput, csv.ToString());
我没有试穿 IDE 但它应该可以满足您的要求。似乎您从某处复制粘贴了该代码,并试图在不理解的情况下对其进行操作。我建议确保您在开始使用它之前逐行理解它为什么使用它,如何使用它。否则你将永远需要有人为你编写代码!
输出将是(excel):
这是我认为你正在尝试做的事情的简化版本。
代码运行前的示例hostheaders.csv
www.google.com
www.whosebug.com
www.fakewebsitehere.com
更新代码
string filePath = @"c:\hostnames.csv";
List<string> results = new List<string>();
using (var reader = new StreamReader(filePath))
{
Ping ping = new Ping();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
var hostname = values[0];
Console.WriteLine(hostname);
PingReply pingresult = ping.Send(hostname);
results.Add($"{line},{pingresult.Status.ToString()}");
}
}
File.WriteAllLines(filePath, results);
hostheaders.csv 代码运行后
www.google.com,Success
www.whosebug.com,Success
www.fakewebsitehere.com,TimedOut
我想对 csv 中的主机名执行 ping 操作,并将结果写在下一列中,但我有点不知所措如何操作?
这是错误 I get:Error CS0021 无法将带有 [] 的索引应用于 'StreamReader'
类型的表达式我唯一能做的就是将 csv 中的内容写入控制台。
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());
}
不清楚您得到的错误是什么,但我注意到一个问题是您没有向 string.Format 提供变量,我认为它在那里没用..
示例 hostnames.csv 我假设是这样的:
www.google.com,www.somebadu-rlwhichcannot..com,www.whosebug.com
var filePath = @"c:\test\hostnames.csv"; // change it to your source
var filePathOutput = @"c:\test\hostnamesOutput.csv"; // use separate output file so you would not overwrite your source..
var ping = new Ping();
var hostNames = new List<string>();
using (var reader = new StreamReader(filePath))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
hostNames = line.Split(',').ToList();
}
}
var goodPing = new List<string>();
foreach (var singleComputer in hostNames)
try
{
var pingResult = ping.Send(singleComputer);
if (pingResult.Status == IPStatus.Success)
{
goodPing.Add(singleComputer);
}
}
catch (Exception e)
{
// host was not in a correct format or any other exception thrown....
// do whatever error handling you want, logging etc...
}
var csv = new StringBuilder();
foreach (var hostname in hostNames)
{
var resultText = goodPing.Contains(hostname) ? "Success" : "Failed";
var newLine = string.Format("{0},{1}", hostname, resultText);
csv.AppendLine(newLine);
}
File.WriteAllText(filePathOutput, csv.ToString());
我没有试穿 IDE 但它应该可以满足您的要求。似乎您从某处复制粘贴了该代码,并试图在不理解的情况下对其进行操作。我建议确保您在开始使用它之前逐行理解它为什么使用它,如何使用它。否则你将永远需要有人为你编写代码!
输出将是(excel):
这是我认为你正在尝试做的事情的简化版本。
代码运行前的示例hostheaders.csv
www.google.com
www.whosebug.com
www.fakewebsitehere.com
更新代码
string filePath = @"c:\hostnames.csv";
List<string> results = new List<string>();
using (var reader = new StreamReader(filePath))
{
Ping ping = new Ping();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
var hostname = values[0];
Console.WriteLine(hostname);
PingReply pingresult = ping.Send(hostname);
results.Add($"{line},{pingresult.Status.ToString()}");
}
}
File.WriteAllLines(filePath, results);
hostheaders.csv 代码运行后
www.google.com,Success
www.whosebug.com,Success
www.fakewebsitehere.com,TimedOut