Search code examples
c#asp.netfirepad

getting an error that 'the process cannot access the file because it is being used by another process' in my dot net program


I tried 'using' but it says that the method is not Idisposable. I checked for running processes in Task Manager, nothing there. My goal is to upload a file from local directory to the Rich Text editor in my website. Please help me resolve this issue. Thanks in Advance

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

Solution

  • FileStream is disposable, so you can use using on it:

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