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

kranthi kumar 206 Reputation points
2020-01-15T13:56:45.687+00:00

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

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Amy Peng-MSFT 251 Reputation points Microsoft Employee
    2020-02-26T14:14:13.767+00:00

    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.


1 additional answer

Sort by: Most helpful
  1. Tom_Cox 1 Reputation point
    2020-04-04T04:26:35.607+00:00

    Long PathTool is a software that will let you easily

    delete, copy or rename long path files.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.