我正在阅读一个文本文件并选择某些关键字并为其添加字符串,然后将其写入另一个空文本文档。我的问题是,因为我对多个字符串执行此操作,所以它会多次写入文本文件,最后为原始文本文档中的每一行添加5行。有人知道我可以使用另一种方法来完成这项工作吗?
var fileContents = System.IO.File.ReadAllLines(textBox5.Text);
var outFileContents = new List<string>();
foreach (var line in fileContents)
{
if (line.Contains("Start **** Connect Process")) //Text to find
outFileContents.Add(line + "," + Environment.NewLine + "*****" + Environment.NewLine); //Add your text
else
outFileContents.Add(line + ","); //Keep column
if (line.Contains("Start $$$$$$ Connect Process"))
outFileContents.Add(line + "," + Environment.NewLine + "$$$$$$" + Environment.NewLine);
else
outFileContents.Add(line + ",");
if (line.Contains("Fail to send &&&&&&&&"))
outFileContents.Add(line + "," + Environment.NewLine + "&&&&&&" + Environment.NewLine);
else
outFileContents.Add(line + ",");
if (line.Contains("Start @@@@@ Process"))
outFileContents.Add(line + "," + Environment.NewLine + "@@@@@@" + Environment.NewLine);
else
outFileContents.Add(line + ",");
if (line.Contains("ConnectionStatus: ######"))
outFileContents.Add(line + "," + Environment.NewLine + "######" + Environment.NewLine);
else
outFileContents.Add(line + ",");
System.IO.File.WriteAllLines(textBox6.Text, outFileContents);
}
Process.Start(textBox6.Text);
}
答案 0 :(得分:3)
问题不在于你是在循环中调用WriteAllLines
。 WriteAllLines
会覆盖文件。只需看the documentation。
问题是你要将每一行添加到输出列表五次。
你拥有的基本上是:
for every line
if ()
add modified line
else
add unmodified line
if ()
add modified line
else
add unmodified line
其中五个条件意味着将五个副本添加到输出列表中。
您需要在临时缓冲区中构建字符串,并将其添加到列表中一次。类似的东西:
for(...)
{
StringBuilder sb = new StringBuilder(line);
if (line.Contains("Start **** Connect Process"))
sb.Append("," + Environment.NewLine + "*****" + Environment.NewLine);
else
sb.Append(',');
// do that for each of your conditionals.
// and finally, add the line to the output buffer:
outFileContents.Add(sb.ToString());
}
现在,您应该将WriteAllLines
移出循环,这样您就不会每次都重写文件。所以你的代码变成了:
for (....)
{
// do stuff
}
File.WriteAllLines(...)
答案 1 :(得分:0)
从foreach中获取此System.IO.File.WriteAllLines(textBox6.Text, outFileContents);
var fileContents = System.IO.File.ReadAllLines(textBox5.Text);
var outFileContents = new List<string>();
foreach (var line in fileContents)
{
}
System.IO.File.WriteAllLines(textBox6.Text, outFileContents);
Process.Start(textBox6.Text);
}
答案 2 :(得分:0)
原因是因为你一遍又一遍地写文字(它在一个循环中)。相反,您需要通过移动WriteAllLines
方法调用在操作结束时只写一次该文件。
此外,您可能需要考虑使用StringBuilder类来生成内容。
var fileContents = System.IO.File.ReadAllLines(textBox5.Text);
var outFileContents = new List<string>();
foreach (var line in fileContents)
{
if (line.Contains("Start **** Connect Process")) //Text to find
outFileContents.Add(line + "," + Environment.NewLine + "*****" + Environment.NewLine); //Add your text
else
outFileContents.Add(line + ","); //Keep column
if (line.Contains("Start $$$$$$ Connect Process"))
outFileContents.Add(line + "," + Environment.NewLine + "$$$$$$" + Environment.NewLine);
else
outFileContents.Add(line + ",");
if (line.Contains("Fail to send &&&&&&&&"))
outFileContents.Add(line + "," + Environment.NewLine + "&&&&&&" + Environment.NewLine);
else
outFileContents.Add(line + ",");
if (line.Contains("Start @@@@@ Process"))
outFileContents.Add(line + "," + Environment.NewLine + "@@@@@@" + Environment.NewLine);
else
outFileContents.Add(line + ",");
if (line.Contains("ConnectionStatus: ######"))
outFileContents.Add(line + "," + Environment.NewLine + "######" + Environment.NewLine);
else
outFileContents.Add(line + ",");
}
System.IO.File.WriteAllLines(textBox6.Text, outFileContents);
Process.Start(textBox6.Text);