How to save a folder from FolderPicker in StorageFolder?

1.5k views Asked by At

When i try to do this:

        folderPicker = new FolderPicker();
        folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
        folderPicker.FileTypeFilter.Add(".txt");
        StorageFolder folder = await folderPicker.PickSingleFolderAsync();

it show me error:

Error 2 The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'. C:\Users\Lukasz\Documents\Visual Studio 2012\Projects\RobimyProjekt\RobimyProjekt\ImageBrowser.xaml.cs.

When i deleted "await" it show me another error:

Error 2 Cannot implicitly convert type 'Windows.Foundation.IAsyncOperation' to 'Windows.Storage.StorageFolder' C:\Users\Lukasz\Documents\Visual Studio 2012\Projects\RobimyProjekt\RobimyProjekt\ImageBrowser.xaml.cs 61 36 RobimyProjekt.

What's going on? That code is from msdna and i using Visual Studio 2012.

4

There are 4 answers

1
AudioBubble On

The below worked for me. It took me a few days to figure this out; but, for my own learning project, I wanted to see if I could make a Folder, File, and then read from it. I was, just, able to create my folder in my designated path by doing the below.

Granted, I am passing a Textbox object as a parameter; but, disregarding that, the following works for me when I try and create my folder using a FolderPicker and a StorageFolder.

public static async Task<string> createDirectory(TextBox parmTextBox)
{
    string folderName = parmTextBox.Text.Trim();

    // Section: Allows the user to choose their folder.
    FolderPicker fpFolder = new FolderPicker();
    fpFolder.SuggestedStartLocation = PickerLocationId.Desktop;
    fpFolder.ViewMode = PickerViewMode.Thumbnail;
    fpFolder.FileTypeFilter.Add("*");
    StorageFolder sfFolder = await fpFolder.PickSingleFolderAsync();

    if (sfFolder.Name != null)
    {
        // Gives the StorageFolder permissions to modify files in the specified folder.
        Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace("CSharp_Temp", sfFolder);

        // creates our folder
        await sfFolder.CreateFolderAsync(folderName);

        // returns a string of our path back to the user
        return string.Concat(sfFolder.Path, @"\", folderName); 
    }
    else
    {
        MessageDialog msg = new MessageDialog("Need to choose a folder.");
        await msg.ShowAsync();
        return "Error: Choose new folder.";
    }
}
0
Muhammad Umar On

Try this. You have to use async keyword for await.

private async void pickFolder(object sender, RoutedEventArgs e)
{
    folderPicker = new FolderPicker();
    folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
    folderPicker.ViewMode = PickerViewMode.List;
    folderPicker.FileTypeFilter.Add(".txt");
    StorageFolder folder = await folderPicker.PickSingleFolderAsync();
    if(folder != null)
    {
         StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
    }

}

0
Dlac On

change it to

private async void (pickFolder(object sender, RoutedEventArgs e)
0
avandeursen On

Listening to the advice in the error message may also be a good idea:

Consider marking this method with the 'async' modifier and changing its return type to 'Task'.

The Task return type makes the method "awaitable".

The void solution as suggested in the other answers works too, but yields a 'fire & forget' solution. Recommended practice is to actually return a Task so that if the caller wishes to do something with, e.g., exceptions resulting from your method this is possible.

To quote from http://msdn.microsoft.com/en-us/magazine/jj991977.aspx:

"To summarize this first guideline, you should prefer async Task to async void. Async Task methods enable easier error-handling, composability and testability."