locked
deleting a folder including the root RRS feed

  • Question

  • hi

    i'm trying to delete a whole folder including the root. i have a folder called A and i'd like to delete the content of the folder (that works perfectly via StorageFolder.DeleteAsync()) and the folder itself. StorageFolder.DeleteAsync() leaves the root folder (A) intact.

    is there way to achieve to delete a folder structure including the root folder? 

    thanks

    Tuesday, March 6, 2012 3:30 PM

Answers

  • Hi Tamas,

    I can't reproduce this problem. In a test app I created a new folder hierarchy in my LocalFolder section. I was able to delete the entire hierarchy including the root without any problem, unless I kept a reference open to the folder in which case I got an UnauthorizedAccessException error. Note that you won't see the error unless you break on the first chance exception, so you might need to enable that in the Debug.Exceptions… dialog.

    Here's my code to create the folders:

    private async void Button_Click_1(object sender, RoutedEventArgs e)
            {
                StorageFolder foo = await ApplicationData.Current.LocalFolder.CreateFolderAsync("foo");
                Debug.WriteLine(foo.Path);
                var bar = await foo.CreateFolderAsync("bar");
                var baz = await bar.CreateFolderAsync("baz");
                var biff = await baz.CreateFileAsync("biff.txt");
            }

    And to delete the folders:

     private async void Button_Click_2(object sender, RoutedEventArgs e)
            {
                StorageFolder foo = await ApplicationData.Current.LocalFolder.GetFolderAsync("foo");
                await foo.DeleteAsync();
            }
    
    --Rob
    Friday, March 9, 2012 6:05 AM
    Moderator

All replies

  • How are you acquiring the StorageFolder to delete, where is the folder located, and what sort of error do you get?

    --Rob

    Wednesday, March 7, 2012 4:37 AM
    Moderator
  • I acquire the folder via ApplicationData.LocalFolder so I have full access to the folders. And I don't get any errors. It works fine, only it does not delete the root folder. 
    Wednesday, March 7, 2012 11:20 AM
  • Hi Tamas,

    I can't reproduce this problem. In a test app I created a new folder hierarchy in my LocalFolder section. I was able to delete the entire hierarchy including the root without any problem, unless I kept a reference open to the folder in which case I got an UnauthorizedAccessException error. Note that you won't see the error unless you break on the first chance exception, so you might need to enable that in the Debug.Exceptions… dialog.

    Here's my code to create the folders:

    private async void Button_Click_1(object sender, RoutedEventArgs e)
            {
                StorageFolder foo = await ApplicationData.Current.LocalFolder.CreateFolderAsync("foo");
                Debug.WriteLine(foo.Path);
                var bar = await foo.CreateFolderAsync("bar");
                var baz = await bar.CreateFolderAsync("baz");
                var biff = await baz.CreateFileAsync("biff.txt");
            }

    And to delete the folders:

     private async void Button_Click_2(object sender, RoutedEventArgs e)
            {
                StorageFolder foo = await ApplicationData.Current.LocalFolder.GetFolderAsync("foo");
                await foo.DeleteAsync();
            }
    
    --Rob
    Friday, March 9, 2012 6:05 AM
    Moderator