Free Support Forum - aspose.com

File access exception happens randomly when saving workbook

Hi,

When I used the following code to read a workbook, copy contents from another workbook, and save to the original file path. I noticed a System.IO.IOException about file access happened randomly.

Code:

        var workbookPath = @"C:\test.xlsx";
        var workbook2path = @"C:\test2.xlsx";

        // can be replicable with this code
        for (int i = 0; i < 1000; ++i)
        {
            var workbook = new Workbook(workbookPath);
            var workbook2 = new Workbook(workbook2path);
            workbook2.Copy(workbook);
            workbook2.Save(workbook2.FileName); // fail randomly
        }

        // ... or this loop
        for (int i = 0; i < 1000; ++i)
        {
            var workbook = new Workbook(workbookPath);
            Workbook workbook2;
            using (var outStream = new FileStream(workbook2path, FileMode.OpenOrCreate))
                workbook2 = new Workbook(outStream);

            workbook2.Copy(workbook);
            workbook2.FileName = workbook2path;
            workbook2.Save(workbook2.FileName); // fail randomly
        }

After a couple of iterations on either of the above loops, it will throw a System.IO.IOException about file access:
image.png (19.9 KB)

I’ve attached the test files for your reference. Could you help me check what went wrong here?

test.zip (21.1 KB)

Thanks,

@ServerSide527,

I tested your exact code with your template files using latest version of Aspose.Cells (e.g Aspose.Cells for .NET v20.9.x), it works fine and I do not find the issue. I run the code twice b/w different internals but still I could not get the exception.The only difference is I used a .NET application created on other drive (e.g E drive) and changed the file paths accordingly. Did you try to run your solution with sample files on a drive other than C drive if it makes any difference. Also, try using latest version/fix if you are not already using it.

Hi @Amjad_Sahi

I put it in different places in the drive but I could still reproduce the issue.

I could even reproduce it by using a simpler code with only one workbook:

var workbook2path = @“C:\test2.xlsx”;
for (; i < 1000; ++i)
{
var workbook2 = new Workbook(workbook2path);
workbook2.Save(workbook2path); // fail randomly
}

I also noticed that if I save the file to a different path and copy it back to the workbook2path, the issue doesn’t happen:

                var tempFile = ".\temp.xlsx";
                workbook2.Save(tempFile);
                File.Copy(tempFile, workbook2path, true);
                workbook2.FileName = workbook2path;

I, however, tried on several machines/servers, and found that on one of the particular machines, the issue didn’t happen. The machine is windows 10 but I could also replicate the issue on other windows 10 machines.

I suspected it was coming from some particular software/hardware compatibility but I couldn’t figure out the exact reason.

In any case, it is weird for Aspose to not able to open and resave the XLSX to the same path. Do you recall any similar issues before or have any further suggestions on what we can check/provide?

Thanks,

@ServerSide527,

I do not recall any similar issue we got before. Maybe you could browse internet for similar scenarios or issues and try the recommendations mentioned in those threads.
e.g




etc.

We will still check if something could be done to avoid such an exception.