在我的点网程序中收到“该进程无法访问该文件,因为该文件正在被另一个进程使用”的错误

问题描述

我尝试了“使用”,但是它说该方法不是一次性的。我检查了任务管理器中正在运行的进程,那里什么也没有。我的目标是将文件从本地目录上传到我网站上的RTF编辑器。请帮助我解决此问题。在此先感谢

public void OnPostUploadDocument()
{
    var projectRootPath = Path.Combine(_hostingEnvironment.ContentRootPath,"UploadedDocuments");
    var filePath = Path.Combine(projectRootPath,UploadedDocument.FileName);
    UploadedDocument.copyTo(new FileStream(filePath,FileMode.Create));

    // Retain the path of uploaded document between sessions.
    UploadedDocumentPath = filePath;

    ShowDocumentContentInTextEditor();

}

private void ShowDocumentContentInTextEditor()
{
    WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
    Editor editor = new Editor(UploadedDocumentPath,delegate { return loadOptions; }); //passing path and load options (via delegate) to the constructor
    EditableDocument document = editor.Edit(new WordProcessingEditOptions()); //opening document for editing with format-specific edit options

    DocumentContent = document.GetBodyContent(); //document.GetContent();
    Console.WriteLine("HTMLContent: " + DocumentContent);

    //string embeddedHtmlContent = document.GetEmbeddedHtml();```
    //Console.WriteLine("EmbeddedHTMLContent: " + embeddedHtmlContent);
}

解决方法

FileStream是一次性的,因此您可以在其上使用

using (var stream = new FileStream(filePath,FileMode.Create)
{
    UploadedDocument.CopyTo(stream);
}