How to delete a file in localstorage on winrt?

I try to delete a file in local storage without success. Exactly, I took a photo, and I want to delete it later using the button for an example. But when I click on the button, the application errors and I have: "access is denied."

I will upload a simple Delet.Async () after I receive the file in StorageFile.

private async void delete_click(object sender, RoutedEventArgs e) { StorageFile filed = await ApplicationData.Current.LocalFolder.GetFileAsync("myImg.jpg"); if (filed != null) { await filed.DeleteAsync(); } } 
+6
source share
3 answers

Try the code below to see if it works for you.

  private async void takephoto_click(object sender, RoutedEventArgs e) { var ui = new CameraCaptureUI(); ui.PhotoSettings.CroppedAspectRatio = new Size(4, 3); var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo); if (file != null) { // store the file var myFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("myImg.jpg"); await file.MoveAndReplaceAsync(myFile); // display the file var bitmap = new BitmapImage(); bitmap.SetSource(await file.OpenAsync(FileAccessMode.Read)); Photo.Source = bitmap; } } private async void delete_click(object sender, RoutedEventArgs e) { StorageFile filed = await ApplicationData.Current.LocalFolder.GetFileAsync("myImg.jpg"); if (filed != null) { await filed.DeleteAsync(); } StorageFile filefound = await ApplicationData.Current.LocalFolder.GetFileAsync("myImg.jpg"); if (filefound != null) { // do something here } } 
+7
source

I have the same problem when deleting a file from local storage. There are many files stored in local storage with different names, therefore, how to delete other files. in the above case, you can hardcode the file name for deletion.

StorageFile filefound = await ApplicationData.Current.LocalFolder.GetFileAsync ("myImg.jpg"); instead of user myImg.jpg, the user wants to share another file, and then as the user deletes

0
source
  /// <summary> /// Delete the indicated application file /// </summary> /// <param name="strFilePathName">The file path name to delete</param> /// <returns>True, if successful; else false</returns> public async static Task<bool> DeleteAppFile(string strFilePathName) { try { StorageFile fDelete = null; if (!strFilePathName.Equals("")) { fDelete = await ApplicationData.Current.LocalFolder.GetFileAsync(strFilePathName); if (fDelete != null) { try { await fDelete.DeleteAsync(); } catch (Exception ex) { AFFECTS.App.ShowMessage(true, "Error", "DeleteAppFile {" + strFilePathName + "}", ex.Message); return false; } return true; } } else AFFECTS.App.ShowMessage(true, "Error", "DeleteAppFile", "File path name is empty."); } catch (Exception ex) { AFFECTS.App.ShowMessage(true, "Error", "DeleteAppFile {" + strFilePathName + "}", ex.Message); } return false; } 
0
source

All Articles