小编s4m*_*dev的帖子

无法从文本文件中替换字符串

我正在尝试编写的代码是替换文本文件中的一串字.虽然我能够将文件的内容读取到控制台,但我无法替换字符串并将新字符串写入文件.

这是我的代码:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是.Replace正在删除文本文件中的所有内容,只插入目录的路径.

.net c#

2
推荐指数
1
解决办法
83
查看次数

标签 统计

.net ×1

c# ×1