c# - UWP StorageFolder 访问下载文件夹

标签 c# .net uwp windows-10 windows-10-universal

我对 MSDN 和 SO 进行了大量研究,但似乎有很多关于这个主题的评论褒贬不一,没有直接的答案。我的 UWP 应用需要为用户下载一些项目。将其放入“下载”文件夹而不是文档或图片似乎是合乎逻辑的。

我从阅读中了解到,允许应用程序访问下载文件夹并在下载文件夹中创建文件和子文件夹。但是,如果不使用选择器,它就无法访问其他文件和文件夹(不是从您的应用程序创建的)。在这种情况下,我不需要使用选择器,因为我的应用正在使用并为自己创建文件夹。我也读过, list 中不需要特殊功能就可以工作。

我可以通过在下载文件夹中创建一个文件夹和一个文件来确认这确实有效

StorageFile destinationFile;
StorageFolder downloadsFolder;
try
{
   //Create a sub folder in downloads
   try
   {
         downloadsFolder = await DownloadsFolder.CreateFolderAsync("AppFiles");
   }
   catch (Exception ex)
   {
       //HERE IS THE ISSUE. I get in here if the folder exists but how do i get it?
   }

   destinationFile = await downloadsFolder.CreateFileAsync(destination,CreationCollisionOption.GenerateUniqueName);
}
catch (FileNotFoundException ex)
{
     rootPage.NotifyUser("Error while creating file: " + ex.Message, NotifyType.ErrorMessage);
     return;
}

然而,这里是主要问题。此代码第一次运行良好,因为该文件夹尚不存在,它会与文件一起创建。随后的时间,它失败并抛出异常:

Cannot create a file when that file already exists. (Exception from HRESULT: 0x800700B7)

它在线上执行此操作以在下载文件夹中创建文件夹:

downloadsFolder = await DownloadsFolder.CreateFolderAsync("AppFiles");

问题是 MSDN 声明我不能使用“OpenIfExists”或“ReplaceExisting”的碰撞选项,这是我需要解决此问题的两个碰撞选项。剩下的两个选项对我没有好处。因此,无论如何,如果该文件夹存在,它将抛出异常。

然后,我的想法是我可以捕获异常,就像我在上面的代码片段中所做的那样,如果文件夹存在则打开它。问题在于“DownloadsFolder”类没有提供任何获取或打开文件夹的选项,仅提供创建文件夹的选项。

那么,似乎我可以从我的应用程序创建文件夹,但我无法打开或获取我的应用程序创建的文件夹?

谢谢!

最佳答案

The problem with this is that the "DownloadsFolder" class does not give any options to get or open a folder, only to create a folder.

实际上,当您第一次运行代码时,您可以成功创建文件夹并获取文件夹实例以在此文件夹中创建文件。但为什么当它存在时您却无法获取它,这是设计使然。

我相信你已经检查了 document :

Because the app can only access folders in the Downloads folder that it created, you can't specify OpenIfExists or ReplaceExisting for this parameter.

那么,如何获取您创建的文件夹?我会在下面告诉你:)

In this case, I should not need to use a picker because my app is using the and creating the folder for itself.

如您所说,第一个选项是使用选择器,但您说过您不想使用选择器。那么,我再给你一个选择。

当您第一次成功创建文件夹时,您可以将此文件夹添加到 FutureAccessList .然后,您可以直接在代码中获取此文件夹。

我做了一个简单的代码示例供您引用:

StorageFile destinationFile;
StorageFolder downloadsFolder;
try
{

    try
    {
        downloadsFolder = await DownloadsFolder.CreateFolderAsync("AppFiles");
        string folderToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(downloadsFolder);
        ApplicationData.Current.LocalSettings.Values["folderToken"] = folderToken;
        destinationFile = await downloadsFolder.CreateFileAsync("destination.txt", CreationCollisionOption.GenerateUniqueName);
    }
    catch (Exception ex)
    {
        if (ApplicationData.Current.LocalSettings.Values["folderToken"] != null)
        {
            string token = ApplicationData.Current.LocalSettings.Values["folderToken"].ToString();
            downloadsFolder = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFolderAsync(token);
            destinationFile = await downloadsFolder.CreateFileAsync("destination.txt", CreationCollisionOption.GenerateUniqueName);
        }
    }

}
catch (FileNotFoundException ex)
{
    rootPage.NotifyUser("Error while creating file: " + ex.Message, NotifyType.ErrorMessage);
    return;
}

关于c# - UWP StorageFolder 访问下载文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50764811/

相关文章:

c# - 在UWP 应用程序中,我可以在建立连接后获取SSL 证书错误列表吗?

c# - 如何在uwp中使用ssl证书

c# - WPF 应用程序 list 文件

c# - 如何在 .net 4.5 中读取连接字符串

.net - 使用 IIS Express 托管网站(临时)

c# - .net 中的 binning-bucketing 数值

c# - 在SQL中将一个数字添加到未知数

c# - 我应该如何使用集合?

c# - SQL 日期时间到 C# 字符串并返回到 SQL 日期时间

c# - 将数据模板内的元素绑定(bind)到父属性