question

kranthikumar-9265 avatar image
0 Votes"
kranthikumar-9265 asked ·

[UWP][C#] Unable to delete "user selected folder" in UWP apps C#

i am trying to delete a folder selected by a user using below snippet,

 var folderPicker = new FolderPicker()
 {
      SuggestedStartLocation = PickerLocationId.DocumentsLibrary
 };
 folderPicker.FileTypeFilter.Add("*");
 var folder = await folderPicker.PickSingleFolderAsync();
 
 await folder.DeleteAsync(StorageDeleteOption.Default);


but i am receiving System.UnauthorizedAcessException , how to resolve it (or) how to properly delete selected folder

uwpuwp-xaml
· 3
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

When I used FolderPicker to choose a folder and tried to delete it, also received the same error. But I'm not sure what caused this unexpected behavior, I'll ask the related teams about it.

0 Votes 0 · ·

Please provide me an update on this as soon as possible.

0 Votes 0 · ·
FayWang-MSFT avatar image FayWang-MSFT kranthikumar-9265 ·

Once I receive any information about this, I will update.

0 Votes 0 · ·
AmyPeng-MSFT avatar image
0 Votes"
AmyPeng-MSFT answered ·

Hi kranthikumar,

Welcome to post your question in Microsoft Q&A!

Thanks for reporting this issue, currently this is a known issue in our internal system.

If you still want to delete folder using FolderPicker, the workaround is that first we need to get the parent folder of the selected folder, then pass the selected folder name to the parent folder to get the folder instance, after that we can delete this selected folder as following:

 var folderPicker = new FolderPicker()
 {
     SuggestedStartLocation = PickerLocationId.DocumentsLibrary
 };
 folderPicker.FileTypeFilter.Add("*");
 var folder = await folderPicker.PickSingleFolderAsync();
 try
 {
     var parent = await folder.GetParentAsync();
     var folderToDelete = await parent.GetFolderAsync(folder.Name);
     await folderToDelete.DeleteAsync();
 }
 


Thanks.

· 2 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This is actually expected behavior. We were able to catch an inner exception from the native Windows code that said this explicitly:

 Exception thrown at 0x00007FFB7639A839 (KernelBase.dll) in RuntimeBroker.exe: WinRT originate error - 0x80070005 : 'Cannot delete or rename the specified folder (New folder). If the folder was acquired using the File Picker or the KnownFolders API, it cannot be deleted or renamed. Additionally, the folder cannot be deleted if it resides in a filtered location, such as the Documents library or removable devices.'.
   

So the answer above is not a workaround, but what you have to do if you want to delete or rename the folder that is picked by the end user with FolderPicker, no matter where on the file system the selected folder resides.

1 Vote 1 · ·

Also make sure you add the documentsLibrary capability to the package manifest using a text editor as that Capability is not available in the GUI editor yet:

 <uap:Capability Name="documentsLibrary"/>

You may also have to add a File Type Assocation in the Declarations page of the package manifest, as per https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions#accessing-additional-locations

Documents documentsLibrary

Note: You must add File Type Associations to your app manifest that declare specific file types that your app can access in this location.

Use this capability if your app:

  • Facilitates cross-platform offline access to specific OneDrive content using valid OneDrive URLs or Resource IDs

  • Saves open files to the user's OneDrive automatically while offline

0 Votes 0 · ·
TomCox-0503 avatar image
0 Votes"
TomCox-0503 answered ·

Long PathTool is a software that will let you easily

delete, copy or rename long path files.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.