During file copy, we have a error "The process cannot access the file 'File.xlsx' because it is being used by another process."

While Copying a file we got below exception. Is there any way to check whether file completely save during workbook.save(path) before cursor moving to next line. I believe by the time it reaches file copy line, the file is still saving. This is an intermittent issue. please suggest any work around on this.

Code:
Workbook workBook = this.LoadTemplate(“FileTemplate.xlsx”);
string reportFilePath = “~/File.xlsx”;
string copyReportFilePath = “~/FileCopy.xlsx”;
workBook.Save(reportFilePath);
File.Copy(reportFilePath, copyReportFilePath, true); // Error occurred here.

Exception message:
The process cannot access the file ‘File.xlsx’ because it is being used by another process.

Stacktrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost) at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)

@emeghana
Which version are you using?
The following codes works fine with the latest version 23.5:

    Workbook workbook = new Workbook(dir + "Freeze.xlsx");

            workbook.Worksheets[0].FreezePanes("B2", 1, 1);
            workbook.Save(dir + "dest.xlsx");
            File.Copy(dir + "dest.xlsx", dir + "d.xlsx", true);

@simon.zhao This issue is not occurring every time . Its intermittent. Its Copying the file sometimes. Its giving the error sometime.

The Aspose.cells version is 20.10.

Is there a way to identify that file is fully saved before copying it.

@emeghana,
After the Workbook.Save method is executed, the save operation is completed.

@emeghana

  1. Do you use it in multithreading?
    2.Please save the file to stream, then close stream by yourself:
 using(FileStream fs = File.Create(dir +"File.xlsx"))
            {
                workbook.Save(fs,SaveFormat.Xlsx);
            }