locked
Check if StorageFolder still exist.

    Question

  • Hi,

    Here is the scenario. In the app, the user can pick a default folder (using the folder picker) in which the files generated by the app will be created. When ready, the user can click/tap a button to create the files. In the code I keep track of the StorageFolder returned by the folder picker and it is also stored in the mostrecentlyusedlist which is used to get the folder back when that app is restarted.

    The issue I'm having is that nothing stops the user from deleting the folder after it's been returned by the folder picker and stored in the StorageFolder variable. If it is deleted while the app is running, the app gives a COM E_FAIL exception when trying to create a file using that StorageFolder. So the question is. Is there a good way to check if the StorageFolder still exist or is still valid before doing any calls on it? Catching the none descriptive COM exception on the create seems a bit strange.

    I can get it to work in a different way but I was just wondering if there is some kind of best practice to avoid these kind of issues.

    Thanks




    • Edited by WarrChosen Tuesday, February 3, 2015 6:40 AM
    Tuesday, February 3, 2015 2:18 AM

Answers

  • I see your point, but I don't know of any way to work around it other than to catch the COMException.  However, you may want to consider locking the folder so it can't be deleted while your app is using it.  Here's some code which will do that:

     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <StackPanel>
                <TextBlock>Instructions:</TextBlock>
                <TextBlock>1) Click the "Choose a folder and lock it" button.</TextBlock>
                <TextBlock>2) Try to delete that folder in Windows Explorer - you should not be able to.</TextBlock>
                <TextBlock>3) Click the "Write to folder close the lock" button.</TextBlock>
                <TextBlock>4) Try to delete that folder again in Windows Explorer - you should be able to now.</TextBlock>
                <Button Content="Choose a Folder and lock it" Click="Button_Click"/>
                <Button Content="Write to Folder and close the lock" Click="Button_Click_1"/>
            </StackPanel>
        </Grid>

    private async void Button_Click(object sender, RoutedEventArgs e)
            {
                FolderPicker F = new FolderPicker();
                F.FileTypeFilter.Add(".txt");
                S = await F.PickSingleFolderAsync();
                T = await S.CreateFileAsync("Temp.txt");
                I = await T.OpenStreamForWriteAsync();
    
            }
    
            private async void Button_Click_1(object sender, RoutedEventArgs e)
            {
                try
                {
                    await S.CreateFileAsync("WeirdFile.txt");
                    I.Dispose();
                    await T.DeleteAsync();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(e.ToString());
                }
            }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, February 3, 2015 1:57 PM
    Moderator

All replies

  • I see your point, but I don't know of any way to work around it other than to catch the COMException.  However, you may want to consider locking the folder so it can't be deleted while your app is using it.  Here's some code which will do that:

     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <StackPanel>
                <TextBlock>Instructions:</TextBlock>
                <TextBlock>1) Click the "Choose a folder and lock it" button.</TextBlock>
                <TextBlock>2) Try to delete that folder in Windows Explorer - you should not be able to.</TextBlock>
                <TextBlock>3) Click the "Write to folder close the lock" button.</TextBlock>
                <TextBlock>4) Try to delete that folder again in Windows Explorer - you should be able to now.</TextBlock>
                <Button Content="Choose a Folder and lock it" Click="Button_Click"/>
                <Button Content="Write to Folder and close the lock" Click="Button_Click_1"/>
            </StackPanel>
        </Grid>

    private async void Button_Click(object sender, RoutedEventArgs e)
            {
                FolderPicker F = new FolderPicker();
                F.FileTypeFilter.Add(".txt");
                S = await F.PickSingleFolderAsync();
                T = await S.CreateFileAsync("Temp.txt");
                I = await T.OpenStreamForWriteAsync();
    
            }
    
            private async void Button_Click_1(object sender, RoutedEventArgs e)
            {
                try
                {
                    await S.CreateFileAsync("WeirdFile.txt");
                    I.Dispose();
                    await T.DeleteAsync();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(e.ToString());
                }
            }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, February 3, 2015 1:57 PM
    Moderator
  • Hi,

    Thanks for the reply.

    I've decided to code the functionality a bit differently in the end. What I'm doing instead is that I just keep track of the token. When I need to use the folder I get the StorageFolder using the token at that point only and check the exceptions then.

    But I still think that the exception mentioned in my previous message should use better exception types and be a bit more descriptive so that it's clear what we are catching. Maybe StorageFolder (StorageItem) should also have a way to check its own status, be it deleted, permission changed, etc...

    Sunday, February 8, 2015 3:33 AM