问题
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.
回答1:
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:
- Check the value of
StorageFile.Path
property and make sure there are no double\
in it and that it indeed points intoApplicationData.Current.LocalFolder
folder. - 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?
回答2:
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.
来源:https://stackoverflow.com/questions/17730536/why-am-i-getting-an-argument-exception-when-attempting-to-delete-a-file