Sporadic “File is being used by another process” when trying to open a file after copying to a network share

In our application we have an ASP.NET web server and an API server for taking on various jobs. In this particular workflow the user uploads a file that the Web server copies to a network share for the Job server to consume. The Job server is notified of this through a new row in an MSSQL table which it scans every second. Sometime this year during integration testing this flow began randomly failing with:

System.IO.IOException: The process cannot access the file '\a\b\c.xlsx' because it is being used by another process."

Locally DEVs and QA never had this issue, but I assume it’s because their shared folders are placed on their respective work machines. When the shared folder is moved from the local file system to a network share, the same exception begins to appear.

I am able to reproduce on a random occasion with this short application:

static void Main(string[] args)
{
    const string DestinationDirectory = "\\10.122.xxx.xxx\Testing";

    var inputPath = args[0];
    var filename = Path.GetFileName(inputPath);
    var destinationPath = Path.Combine(DestinationDirectory, filename);

    Console.WriteLine(filename);

    Console.WriteLine("Copying the source file...");
    File.Copy(inputPath, destinationPath, true);

    Console.WriteLine("Opening the destination file...");
    try
    {
        using var _ = File.Open(destinationPath, FileMode.Open, FileAccess.Read);
        Console.WriteLine("Success!");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}

If I run it with this batch script I hit the exception roughly 1 in 10 times.

@echo off

for /l %%x in (1, 1, 10) do (
    FileShareDemo.exe "C:abcmyfile"
)
myfile
Copying the source file...
Opening the destination file...
System.IO.IOException: The process cannot access the file '\10.122.xxx.xxxTestingmyfile' because it is being used by another process.
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
   at System.IO.File.Open(String path, FileMode mode, FileAccess access)
   at FileShareDemo.Program.Main(String[] args)
  • From my testing, it seems that the file gets locked for a tiny amount of time, hence the sporadic nature.
  • Issue present on .NET 6 and .NET 8.
  • It could well be a Windows related issue, but running this batch script:
@echo off

set file=myfile
set source="C:abc%file%"
set destination="\10.122.xxx.xxxTesting%file%"

for /l %%x in (1, 1, 10) do (
    echo Copying...
    copy %source% %destination%
    
    echo Reading...
    type %destination%
    REM powershell -command "Get-Content %destination% -Encoding byte -TotalCount 2"
)

which performs the same commands, does not trigger the error, so I’m led to believe it’s .NET related.

I have tried to use the Handle utility from Microsoft to find all file associated handles before and after file open, but the application is too slow to register them.

New contributor

Rob P. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật