gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:22:48 24 4
gpt4 key购买 nike

我对 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com