我正在使用 File 类来编辑 HTML 文件。我需要从中删除一行代码。我这样做的方式是:
if (selectedFileType.Equals("html"))
{
string contentsOfHtml = File.ReadAllText(paramExportFilePath);
//delete part that I don't want
string deletedElement = "string I need to delete";
contentsOfHtml.Replace(deletedElement, "");
File.WriteAllText(paramExportFilePath, contentsOfHtml);
}
但是它抛出了异常:The process cannot access the file 'path\to\file.html' because it is being used by another process.
我担心这种情况正在发生,因为File.ReadAllText
orFile.WriteAllText
方法正在文件上运行,即使在文档中它指定它们确实关闭了文件。那么有人知道是什么原因造成的吗?