we wrote a tool to open an excel-file, fill it with datas from a database and save it with another name with closedxml.excel in c#.
Sometimes it throws this error:

The process cannot access the file '' because it is being used by another process.
And I'm wondering, which process can this be, because this file is brand new.

So i wrote a small script to test this.

  1. run
  • open a file and save it 100 times on a local drive
    this is the code:
        var path = @"c:\tmp\CCI_Reporting";
        for (var i = 1; i < 100; i++)
            try
            {
                var workBook = new XLWorkbook($@"{path}\0.xlsx");
                workBook.SaveAs($@"{path}\{i}.xlsx");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

  • result: No error
  1. run
  • open a file and save it 100 times on a network drive
    same code, another path -> e:\CCI_Reporting (network-drive to the local folder)
  • result:
    The process cannot access the file 'E:\CCI_Reporting\1.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\20.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\36.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\40.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\48.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\49.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\51.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\53.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\58.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\59.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\63.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\72.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\87.xlsx' because it is being used by another process.
  1. run
  • open file, wait 10 seconds and save it to the network drive
    add this line Thread.Sleep(10000); before saving it.
  • result:
    The process cannot access the file 'E:\CCI_Reporting\1.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\48.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\58.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\72.xlsx' because it is being used by another process.
    The process cannot access the file 'E:\CCI_Reporting\87.xlsx' because it is being used by another process.
  1. run
  • same like 1. run
  • result: no errors.

There is definitely no other process.
What's the problem here while saving on a network drive?
And what's a possible workaround?
Thank's for help.

1

There are 1 best solutions below

0
Sorrenia On

Why doesn't the person write what is wrong with my question? Unfortunately, a -1 does not help me.

But fortunately I found someone who could help me. And here is the solution:

    var path = @"e:\CCI_Reporting";
    for (var i = 1; i <= 100000; i++)
        try
        {
            using var x = File.Open($@"{path}\0.xlsx", FileMode.Open);
            using var y = File.Create($@"{path}\{i}.xlsx");
            var workBook = new XLWorkbook(x);
            workBook.SaveAs(y);
            Console.WriteLine($"workbook {i} saved.");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }