System.IO.File.Copy sometimes results in System.IO.IOException: Invalid access to memory location.

Andreas Leeb 50 Reputation points


occasionally, after several successful calls to System.IO.File.Copy, the method fails with the following exception:

System.IO.IOException: Invalid access to memory location.
   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)

So the LastWin32Error is 998 (0x000003E6) = ERROR_NOACCESS = Invalid access to memory location.

I cannot find any information on this error occuring in the CopyFile function of kernel32.dll. The funny thing is, from another dummy .NET app started in parallel, the exact same copying works. Also System.IO.File.WriteAllBytes(dest, System.IO.File.ReadAllBytes(source)); works even when System.IO.File.Copy(source, dest); throws this exception.

ProcessMonitor just shows the source file being succesfully opened for reading and then being closed again, and no operation on the destination file.

I'm using .NET Framework 4.8

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,957 questions
Microsoft Technologies based on the .NET software framework.
3,530 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,493 questions
{count} votes

Accepted answer
  1. Pushpraj Singh Rathore 85 Reputation points

    This issue is already noted down in the Known issue notes of May 23, 2023—KB5026435 (OS Build 19045.3031) Preview and the solution proposed is resolved using Known Issue Rollback (KIR).

    You might have intermittent issues saving, copying, or attaching files using 32-bit apps, which are large address aware and using the CopyFile API

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful