UWP app System.IO.PathTooLongException when executing CopyAsync

Lasanga 1 Reputation point
2021-09-04T14:39:07.623+00:00

I am trying to copy captured images and selected images with a file picker to a location inside App data using the StorageFile.CopyAsync() method as below.

    await photo.CopyAsync(destinationFolder, fileName, NameCollisionOption.ReplaceExisting);

I am getting below exception when I try to call photo.CopyAsync() for an image file

"System.IO.PathTooLongException: The specified file name or path is too long, or a component of the specified path is too long."

the destination path:

C:\Users\jaimecollins\AppData\Local\Packages\73ea377b-f9f3-4a64-bb17-f937c082246a_x73cm3athdc94\LocalState\17efbdca-9e18-48c1-b203-a76a71775730\5b058cfc-f7f1-42ec-afb3-4ee6b9804e0f\89d6baaa-cbc6-4ae1-8830-82dfbe14ade1

file name: 09932aeb-be37-425a-aafb-af8e2eace75f.jpg

length of the destination path is 217 and after copying with the file name it will be 258 which is less than the Max_Path length of 260

when the destination path with 212 and with the file 253 as below, there is no exceptions and the file is getting copied.
C:\Users\lasanga\AppData\Local\Packages\73ea377b-f9f3-4a64-bb17-f937c082246a_x73cm3athdc94\LocalState\17efbdca-9e18-48c1-b203-a76a71775730\5b058cfc-f7f1-42ec-afb3-4ee6b9804e0f\89d6baaa-cbc6-4ae1-8830-82dfbe14ade1

Please let me know the what is the limit for the path names and if there is any workaround to support long file paths

Thanks.

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 81,446 Reputation points
    2021-09-04T15:39:29.44+00:00

    On my OS (Windows 10 1909), I can create a path+file > 300 with CopyAsync and LongPathsEnabled = 1 in registry, as explained at
    Maximum Path Length Limitation