1

I am trying to overwrite a file with a new version inside of an AppData sub-directory.

Current process is that the user has to select the File to overwrite, and the folder directory it sits in separately so that I can add them to the future access list. Later on the user can select from a collection of images, and it's at that point that it needs to copy and overwrite the destination file. The code I've tried to do this is as follows:

// lets try to copy file to wallpaper default location.
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
var faToken = localSettings.Values["WallpaperStorageFileFAToken"] as string;
var faTokenFolder = localSettings.Values["WallpaperStorageFolderFAToken"] as string;

var destinationFile = await LoadExistingFileSelection(faToken);
var destinationFolder = await LoadExistingFolderSelection(faTokenFolder);

StorageFile movedFile = null;
try
{
    movedFile = await imageFile.CopyAsync(destinationFolder, destinationFile.Name, NameCollisionOption.ReplaceExisting);
}
catch (Exception genEx)
{
    //
}

The Fa tokens are taken after an FileOpenPicker is used by the User to get the StorageFile and a FolderPicker used to get the StorageFolder for the directory of the destination.

LoadExistingFolderSelection and LoadExistingFileSelection use the following bits of code to get the StorageFiles and StorageFolder

await StorageApplicationPermissions.FutureAccessList.GetFileAsync(faToken);

and

await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(faFolderToken);

The issue is that this line:

imageFile.CopyAsync(destinationFolder, destinationFile.Name, NameCollisionOption.ReplaceExisting);

Throws this error:

"Value does not fall within the expected range."

and that's it, literally nothing else, any thoughts would be great.

JoeTomks
  • 3,243
  • 1
  • 18
  • 42
  • 1
    When I used your code to test it, it worked well. Can you show the complete code for us to reproduce it? – Faywang - MSFT Sep 12 '19 at 04:05
  • @Faywang-MSFT I may try to put it all into a git repo at some point today, but is there any chance this is a permissions issue, I mean I can copy files directly into the appdata folder I'm trying to overwrite the file in manually. – JoeTomks Sep 12 '19 at 11:28
  • 1
    The app's localfolder directory can read and write, it won't exist permissions issue. – Faywang - MSFT Sep 13 '19 at 06:12

1 Answers1

2

Backslashes... Honestly this is one of those moments in you're development career where you just sit there and sigh, because it's just so dumb.

So I could happily retrieve the image I wanted to copy absolutely fine into a StorageFile object, with a path that contained 1 additional backslash that the Windows OS File Explorer wouldn't have batted an eye lid about.

No problem so far, great proceed to then run a copy operation against that, and suddenly you get the

“Value does not fall within the expected range.”

exception, argue-ably one of the least helpful exceptions relating to a problem with a file path that I've seen.

So there you have it, backslashes, really carefully examine the Path property. Hindsight. Normally I would just remove that question because this answer isn't particularly any kind of great revelation. But I figure it can stay as a warning about the fearsome backslash and UWP's Storage API.

JoeTomks
  • 3,243
  • 1
  • 18
  • 42