【发布时间】:2019-04-03 09:51:21
【问题描述】:
我正在尝试编写的代码是替换文本文件中的一串单词。虽然我能够将文件的内容读取到控制台,但我无法替换字符串并将新字符串写入文件。
这是我的代码:
private static void filesys_created (object sender, FileSystemEventArgs e)
{
using (StreamReader sr = new StreamReader(e.FullPath))
{
Console.WriteLine(sr.ReadToEnd());
File.ReadAllText(e.FullPath);
sr.Close();
}
using (StreamWriter sw = new StreamWriter(e.FullPath))
{
string text = e.FullPath.Replace("The words I want to replace");
string newtext = "text I want it to be replaced with";
sw.Write(e.FullPath, text);
sw.Write(newtext);
sw.Close();
}
}
问题是 .Replace 删除了文本文件中的所有内容,只插入了目录的路径。
【问题讨论】: