StorageFile.CopyAsync() fails because of double-slashed path in uwp...help?
-
So I'm trying to copy some data files from my application install directory to the application data directory when the user saves edits to them (because you can't write to the application install directory). Note: this is a UWP app.
The code looks like:
private async Task<StorageFile> CopyFileToLocalDirectory(DataFile type) { var file = await GetModifiedFile(type); if (file == null) { var defaultFile = await GetDefaultFile(type); var folder = ApplicationData.Current.LocalFolder; file = await defaultFile.CopyAsync(folder, defaultFile.Name, NameCollisionOption.ReplaceExisting); } localFiles[type] = file; return file; }
The
defaultFile.CopyAsync
call fails with aSystem.ArgumentException
(message: The argument is not in the required range (or something like that)). I've managed to track down that it's barfing because it can't find the folder I handed it, despite that being a folder that's guaranteed to exist.More sleuthing discovered that CopyAsync fails horribly if the path has double slashes
\\
in it. And the value returned by that API call...has double slashes in it.I can't find any other way of doing this. Am I just screwed then? Help?
-
@benjamin-hall said in StorageFile.CopyAsync() fails because of double-slashed path in uwp...help?:
More sleuthing discovered that CopyAsync fails horribly if the path has double slashes \ in it.
Is it a UNC? for a NTFS path? (or other)
-
@thecpuwizard said in StorageFile.CopyAsync() fails because of double-slashed path in uwp...help?:
@benjamin-hall said in StorageFile.CopyAsync() fails because of double-slashed path in uwp...help?:
More sleuthing discovered that CopyAsync fails horribly if the path has double slashes \ in it.
Is it a UNC? for a NTFS path? (or other)
The filesystem is NTFS, if that's what you're asking.
It's just what VS reports for
ApplicationData.Current.LocalFolder.Path
, running locally on my Windows10 box.I've found a () workaround--it only fails if I try to copy a file from my application install directory. If I just slurp in the text (which I had to do anyway) and write a new file with that same text (basically manually copying that file), it seems to work--no exceptions and the files are on disk where they're supposed to be.
-
@benjamin-hall Note that it helps when testing if changes are persisted properly to uncheck the
uninstall and reinstall package on debug
option...that has a nagging tendency to blow away any stored state.
-
Seems to have had a similar problem, but he was trying to use GetFileFromPathAsync and eventually fixed it with CopyAsync?
-
@izzion That's the exact opposite problem. I can read them just fine...it's
CopyAsync
that was barfing. I've worked around it as above (manually load the text and write it to a new file, because I can create files just fine. Just not copy them for some reason)
-
@benjamin-hall
Obviously, the real solution is to switch to SSDS.
-
@izzion said in StorageFile.CopyAsync() fails because of double-slashed path in uwp...help?:
@benjamin-hall
Obviously, the real solution is to switch to SSDS.Reported for (psychic) assault. Almost serious here, that's low, man, just low.
-
Path.GetFullPath
should fix this for you.Edit: The reason I say this is because
Path.GetFullPath
canonicalizes the path, so it should remove double slashes/backslashes.
-
@benjamin-hall said in StorageFile.CopyAsync() fails because of double-slashed path in uwp...help?:
@thecpuwizard said in StorageFile.CopyAsync() fails because of double-slashed path in uwp...help?:
@benjamin-hall said in StorageFile.CopyAsync() fails because of double-slashed path in uwp...help?:
More sleuthing discovered that CopyAsync fails horribly if the path has double slashes \ in it.
Is it a UNC? for a NTFS path? (or other)
The filesystem is NTFS, if that's what you're asking.
\\Server\Share\Folder\File is a UNC
C:\Folder\\Folder\File is a malformed NTFS path.
[mod: fixed Markdown bullshit -bl]
-
@thecpuwizard this was
C:\\users\\foo\\bar
All the slashes were doubled. May have been the other slashes, whichever is normal for NTFS.
-
@benjamin-hall said in StorageFile.CopyAsync() fails because of double-slashed path in uwp...help?:
@thecpuwizard this was
C:\\users\\foo\\bar
All the slashes were doubled. May have been the other slashes, whichever is normal for NTFS.
If you are looking at the string with a C# debugger, remember that characters will be shown as escaped (so that you could copy/paste what is shown into source code) and not as the actual representation of the string.
IF the characters are actually doubled (length is 19 rather than 16 for your example) then it is indeed an invalid NTFS path and you are well into the land of undefined behavior.
-
@thecpuwizard I was indeed looking at the debugger. That leads me to believe that the issue is elsewhere, that the
StorageFile
object given for the install location has some bit set somewhere that prevents copying elsewhere unless a magic incantation is recited at the dark of a full moon, while dancing widdershins around a bonfire.
-
@benjamin-hall said in StorageFile.CopyAsync() fails because of double-slashed path in uwp...help?:
while dancing widdershins around a bonfire
Which way does the sun normally rotate around a bonfire?? :face_with_stuck-out_tongue_winking_eye:
But, yes, it could easly be something in the (Effective) "permissions" on the StorageFile.
-
@thecpuwizard There's also always the possility of dancing on the surface of a Klein bottle.
-
@rhywden said in StorageFile.CopyAsync() fails because of double-slashed path in uwp...help?:
@thecpuwizard There's also always the possility of dancing on the surface of a Klein bottle.
But it is so hard to decide between the inside and outside surfaces (at least for 4 dimensional beings)