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)