简体   繁体   English

c#替换文件中的字符串

[英]c# replace string within file

String.Replace doesn't seem to work properly when replacing a portion of an HTML file's content. 替换HTML文件内容的一部分时,String.Replace似乎无法正常工作。 For example, String.Replace replaces </body></html> with blah blah blah </body></html> html> - notice the second HTML closing tag is not properly closed and therefore shows up when the page is rendered in the browser by the user. 例如,String.Replace用blah blah blah </body></html> html>替换</body></html> blah blah blah </body></html> html> - 注意第二个HTML结束标记未正确关闭,因此在页面呈现时显示用户的浏览器。

Anyone know why it's not working as intended? 任何人都知道它为什么不按预期工作?

StreamReader sr = fi.OpenText;
String fileContents = sr.ReadToEnd();
sr.close();
fileContents = fileContents.Replace("<body>", "<body onload='jsFx();' />");
fileContents = fileContents.Replace("</body>","blah blah blah </body>");

StreamWriter sw = new StreamWriter(fi.OpenWrite());
sw.WriteLine(contents);
sw.close();

I might rewrite your bit of code like this: 我可能会像这样重写你的代码:

var fileContents = System.IO.File.ReadAllText(@"C:\File.html");

fileContents = fileContents.Replace("<body>", "<body onload='jsFx();' />"); 
fileContents = fileContents.Replace("</body>","blah blah blah </body>"); 

System.IO.File.WriteAllText(@"C:\File.html", fileContents);

I should note that this solution is fine for files of reasonable size. 我应该注意,这个解决方案适用于合理大小的文件。 Depending on hardware, any thing under a few tens of MB. 取决于硬件,任何低于几十MB的东西。 It loads the entire contents into memory. 它将整个内容加载到内存中。 If you have a really large file you may need to stream it through a few hundred KB at a time to prevent an OutOfMemoryException. 如果你有一个非常大的文件,你可能需要一次流几百KB来防止OutOfMemoryException。 That makes things a bit more complicated, since you'd need to also check the break between each chunk to see if split your search string. 这使得事情变得更复杂,因为您还需要检查每个块之间的中断以查看是否拆分了搜索字符串。

There's nothing wrong with string.Replace here. string.Replace在这里没什么问题。

What is wrong is that you're overwriting the file but not truncating it... so if you changed your writing code to just 什么错的,你覆盖文件,但不截断它......所以如果你改变了你写代码,只是

sw.WriteLine("Start");

you'd see "Start" and then the rest of the file. 你会看到“开始”,然后是文件的其余部分。

I would recommend that you use File.ReadAllText and File.WriteAllText instead (take the path from the FileInfo ). 我建议您使用File.ReadAllTextFile.WriteAllText (从FileInfo获取路径)。 That way: 那样:

  • It will completely replace the file, instead of just overwriting 它将完全替换文件,而不是仅覆盖
  • You don't need to worry about closing the reader/writer/stream properly (which you're not doing now - if an exception occurs, you're leaving the reader or writer open) 您无需担心正确关闭读取器/写入器/流(您现在没有这样做 - 如果发生异常,您将使读取器或写入器保持打开状态)

If you really want to use the FileInfo methods, use FileInfo.Open(FileMode.Create) which will truncate the file. 如果您确实想使用FileInfo方法,请使用FileInfo.Open(FileMode.Create)来截断该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM