【问题标题】:How to save a folder from FolderPicker in StorageFolder?如何从 StorageFolder 中的 FolderPicker 保存文件夹?
【发布时间】:2013-09-02 11:43:39
【问题描述】:

当我尝试这样做时:

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

它显示错误:

错误 2 'await' 运算符只能在异步方法中使用。 考虑使用“异步”修饰符标记此方法并更改 其返回类型为“任务”。 C:\Users\Lukasz\Documents\Visual Studio 2012\Projects\RobimyProjekt\RobimyProjekt\ImageBrowser.xaml.cs.

当我删除“等待”时,它显示另一个错误:

错误 2 无法隐式转换类型 'Windows.Foundation.IAsyncOperation' 到 'Windows.Storage.StorageFolder' C:\Users\Lukasz\Documents\Visual 工作室 2012\Projects\RobimyProjekt\RobimyProjekt\ImageBrowser.xaml.cs 61 36 RobimyProjekt。

发生了什么事?该代码来自 msdna,我使用 Visual Studio 2012。

【问题讨论】:

  • 你的函数(包含这段代码)是如何定义的?
  • private void pickFolder(object sender, RoutedEventArgs e){ //之前帖子的代码 }

标签: c# storagefolder


【解决方案1】:

试试这个。您必须使用 async 关键字来等待。

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);
    }

}

【讨论】:

    【解决方案2】:

    以下内容对我有用。我花了几天时间才弄明白。但是,对于我自己的学习项目,我想看看我是否可以制作一个文件夹、文件,然后从中读取。我只是能够通过执行以下操作在我指定的路径中创建我的文件夹。

    当然,我将一个文本框对象作为参数传递;但是,不管这一点,当我尝试使用FolderPickerStorageFolder 创建我的文件夹时,以下内容对我有用。

    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.";
        }
    }
    

    【讨论】:

    • 感谢 @zx485 编辑我的一些空白区域以及“a FolderPicker”和“a StorageFolder”
    【解决方案3】:

    改成

    private async void (pickFolder(object sender, RoutedEventArgs e)
    

    【讨论】:

      【解决方案4】:

      听取错误消息中的建议也可能是个好主意:

      考虑用 'async' 修饰符标记此方法,并将其返回类型更改为 'Task'。

      Task 返回类型使方法“可等待”。

      其他答案中建议的void 解决方案也有效,但会产生“解雇并忘记”解决方案。推荐的做法是实际返回一个Task,这样如果调用者想要做一些事情,例如,你的方法导致的异常,这是可能的。

      引用http://msdn.microsoft.com/en-us/magazine/jj991977.aspx:

      “总结第一个准则,您应该更喜欢 async Task 而不是 async void。Async Task 方法可以更轻松地处理错误、可组合性和可测试性。”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-09
        • 2022-01-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多