簡體   English   中英

嘗試刪除文件時為什么會出現參數異常?

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

我正在運行以下代碼來刪除存在的文件:

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

 }

當它運行file.DeleteAsync(); 給出一個ArgumentException ,消息Value does not fall within the expected range.

我無法在任何地方找到任何信息,為什么我會得到這個。 有任何想法嗎?

調用堆棧:

在Windows的.Storage.StorageFile.DeleteAsync()中的Lightning.Services.DownloaderService.d__36.MoveNext()在e:\\\\ Services \\ DownloaderService.cs:第120行

第120行是DeleteAsync行。

我懷疑你的path值有問題。 我已經寫了一篇關於這個主題的博客文章 ,因為WinRT異常可能非常不一致和誤導。

我建議兩件事來幫助你了解事情:

  1. 檢查StorageFile.Path屬性的值,並確保其中沒有double \\ ,它確實指向ApplicationData.Current.LocalFolder文件夾。
  2. 嘗試使用相同的路徑調用FileIO.ReadBufferAsync 你可能會得到一個更好的例外。

另外,如果您已經擁有完整路徑而不是操縱字符串,為什么不簡單地調用StorageFile.GetFileFromPathAsync

我的猜測是你在你想要刪除的路徑上留下一個斜線 - 你正在替換文件夾路徑,而iirc不包括尾部斜杠。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM