到目前为止我的代码
StreamReader reading = File.OpenText("test.txt");
string str;
while ((str = reading.ReadLine())!=null)
{
if (str.Contains("some text"))
{
StreamWriter write = new StreamWriter("test.txt");
}
}
我知道如何找到文本,但我不知道如何用我自己的文本替换文件中的文本。
最佳答案
读取所有文件内容。使用 String.Replace
进行替换。将内容写回文件。
string text = File.ReadAllText("test.txt");
text = text.Replace("some text", "new value");
File.WriteAllText("test.txt", text);
关于c# - 如何查找和替换文件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44026569/