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

Andreas Leeb 50 Reputation points
2023-05-10T15:14:32.7666667+00:00

Hello,

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 for business Windows Client for IT Pros User experience Other
Developer technologies .NET Other
Developer technologies C#
{count} votes

Accepted answer
  1. Pushpraj Singh Rathore 85 Reputation points
    2023-05-26T13:47:52.2566667+00:00

    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).

    https://support.microsoft.com/en-au/topic/may-23-2023-kb5026435-os-build-19045-3031-preview-2751b693-5544-4110-bc0c-feb8dd7336b3

    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.