C# 获取的错误为';该进程无法访问该文件,因为另一进程正在使用该文件;在我的网络程序中

C# 获取的错误为';该进程无法访问该文件,因为另一进程正在使用该文件;在我的网络程序中,c#,asp.net,firepad,C#,Asp.net,Firepad,我尝试过“使用”,但它说该方法不可识别。我在任务管理器中检查了是否正在运行进程,但什么都没有。我的目标是将一个文件从本地目录上传到我网站的富文本编辑器。请帮我解决这个问题。提前谢谢 public void OnPostUploadDocument() { var projectRootPath = Path.Combine(_hostingEnvironment.ContentRootPath, "UploadedDocuments"); var filePa

我尝试过“使用”,但它说该方法不可识别。我在任务管理器中检查了是否正在运行进程,但什么都没有。我的目标是将一个文件从本地目录上传到我网站的富文本编辑器。请帮我解决这个问题。提前谢谢

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);
}

处置
文件流