2

我想逐行读取文本文件并编辑特定行。因此,我已将文本文件放入一个字符串变量中,例如:

string textFile = File.ReadAllText(filename);

我的文本文件是这样的:

Line A
Line B
Line C
Line abc
Line 1
Line 2
Line 3

我有一个特定的字符串 (="abc"),我想在这个 textFile 中搜索它。所以,我正在阅读这些行,直到找到字符串并在找到的字符串之后转到第三行(“第 3 行”-> 这行总是不同的):

string line = "";
string stringToSearch = "abc";

using (StringReader reader = new StringReader(textFile))
{
    while ((line = reader.ReadLine()) != null)
    {
        if (line.Contains(stringToSearch))
        {
            line = reader.ReadLine();
            line = reader.ReadLine();
            line = reader.ReadLine();

            //line should be cleared and put another string to this line.
        }
    }
}

我想清除第三个读取行并将另一个字符串放入该行并将整个保存stringtextFile.

我怎样才能做到这一点?

4

4 回答 4

2

您可以像这样存储内容StringBuilder

StringBuilder sbText = new StringBuilder();
using (var reader = new System.IO.StreamReader(textFile)) {
    while ((line = reader.ReadLine()) != null) {
        if (line.Contains(stringToSearch)) {
            //possibly better to do this in a loop
            sbText.AppendLine(reader.ReadLine());
            sbText.AppendLine(reader.ReadLine());

            sbText.AppendLine("Your Text");
            break;//I'm not really sure if you want to break out of the loop here...
        }else {
            sbText.AppendLine(line);
        }
    }
}  

然后像这样写回去:

using(var writer = new System.IO.StreamWriter(@"link\to\your\file.txt")) {
    writer.Write(sbText.ToString());
}

或者,如果您只是想将其存储在字符串中,textFile您可以这样做:

textFile = sbText.ToString();
于 2017-08-30T10:41:38.973 回答
1

再次编写整个文件会更容易:

string old  = "abc";
string nw   = "aa";
int counter = 0;

using(StreamWriter w = new StreamWriter("newfile")
{
    foreach(string s in File.ReadLines(path))
        w.WriteLine(s == old ? nw : s);
}
于 2017-08-30T10:38:13.510 回答
1

您可能需要以下内容:

DirectoryInfo di = new DirectoryInfo(Location);
FileInfo[] rgFiles = di.GetFiles("txt File");

foreach (FileInfo fi in rgFiles)
{
    string[] alllines = File.ReadAllLines(fi.FullName);

    for (int i = 0; i < alllines.Length; i++)
    {
        if (alllines[i].Contains(stringToSearch))
        {                        
            alllines[i] = alllines[i].Replace(stringToSearch, some value );
        }
    }
}

这样,您将逐行读取文本文件,直到文档结束,如果该值被拾取,它将被您的新值替换。

于 2017-08-30T10:40:18.960 回答
1

这是一个完整的例子:

using System;
using System.IO;

namespace rename
{
    class Program
    {
        static void Main(string[] args)
        {
            // Fix files, replace text
            DirectoryInfo di = new DirectoryInfo(@"C:\temp\all\");
            FileInfo[] rgFiles = di.GetFiles("*");

            foreach (FileInfo fi in rgFiles)
            {
                string[] alllines = File.ReadAllLines(fi.FullName);

                for (int i = 0; i < alllines.Length; i++)
                {
                    if (alllines[i].StartsWith("00:"))
                    {
                        // Edit: Replace these lines with an empty line
                        alllines[i] = alllines[i].Replace(alllines[i], "");
                    }
                }

                // Rewrite new files in the folder
                File.WriteAllLines(@"C:\temp\new\" + fi.Name, alllines);
            }
        }
    }
}
于 2020-12-27T18:16:27.360 回答