KallDrexx
KallDrexx

Reputation: 27813

Why am I getting an argument exception when attempting to delete a file?

I'm running the following code to delete a file that does exist:

 try
 {
      var folder = ApplicationData.Current.LocalFolder;
      var path = rendition.OfflineLocation.Replace(folder.Path, "");
      var file = await folder.GetFileAsync(path);
      await file.DeleteAsync();
 }
 catch (FileNotFoundException)
 {

 }

When this runs the file.DeleteAsync(); gives an ArgumentException, with the message Value does not fall within the expected range.

I can't find any information anywhere why I would be getting this. Any ideas?

Call Stack:

at Windows.Storage.StorageFile.DeleteAsync() at Lightning.Services.DownloaderService.d__36.MoveNext() in e:\\Services\DownloaderService.cs:line 120

Line 120 is the DeleteAsync line.

Upvotes: 1

Views: 1707

Answers (2)

Damir Arh
Damir Arh

Reputation: 17865

I suspect there's something wrong with your path value. I've already written a blogpost on this subject because WinRT exceptions can be pretty inconsistent and misleading.

I'd suggest two things to help you get to the bottom of things:

  1. Check the value of StorageFile.Path property and make sure there are no double \ in it and that it indeed points into ApplicationData.Current.LocalFolder folder.
  2. Try calling FileIO.ReadBufferAsync with the same path. You might get a better exception.

Also, why not simply call StorageFile.GetFileFromPathAsync if you already have the full path instead of manipulation the string?

Upvotes: 4

JerKimball
JerKimball

Reputation: 16934

My guess is you're leaving a slash prepended to the path you are trying to delete - you're replacing the folder path, which iirc does not include the trailing slash.

Upvotes: 0

Related Questions