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

夙愿已清 提交于 2019-12-11 11:26:02

问题


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:

  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?




回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!