简体   繁体   English

UWP - 获取用户下载文件夹的路径

[英]UWP - Get path to user download folder

I have been looking for a little while now and am not finding much help via MSDN resources and others.我一直在寻找一段时间,并没有通过 MSDN 资源和其他资源找到太多帮助。

My predicament is simple: my app needs a base directory to the Downloads folder.我的困境很简单:我的应用程序需要一个下载文件夹的基本目录。 I am aware of the DownloadsFolder class however that is not suiting my needs currently.我知道DownloadsFolder类,但是目前不适合我的需要。

How do I get the current user's Download folder path in a Windows Universal App?如何在 Windows 通用应用程序中获取当前用户的下载文件夹路径?

Use Windows.Storage.UserDataPaths to get the path of user's download folder.使用Windows.Storage.UserDataPaths获取用户下载文件夹的路径。

string downloadsPath = UserDataPaths.GetDefault().Downloads;
  • This method is introduced in build 16232, so clients with RS3(1709) or later will be able to run it.此方法在版本 16232 中引入,因此具有 RS3(1709) 或更高版本的客户端将能够运行它。
  • You shouldn't obtain downloads folder path using LocalFolder, which might result in wrong folder when the user changed the default location for it .您不应该使用 LocalFolder 获取下载文件夹路径,这可能会在用户更改其默认位置时导致错误的文件夹。

Is that what you need?那是你需要的吗?

string localfolder = ApplicationData.Current.LocalFolder.Path;
var array = localfolder.Split('\\');
var username = array[2];
string downloads = @"C:\Users\" + username + @"\Downloads";

This will result这将导致

C:\\Users\\username\\Downloads C:\\用户\\用户名\\下载

System.Environment.ExpandEnvironmentVariables("%userprofile%/downloads/")

The DownloadsFolder for an app now defaults to a folder withing the user's Downloads directory named after the app name (in actual fact the app name folder is simply a link to a folder named after the Package Family Name ) To get the folder name, I used the following hack (vb) to first create a dummy file in the UWP app's DownloadsFolder then using .NET code to get the directory name, and finally deleting the dummy file.应用程序的DownloadsFolder现在默认为一个文件夹,其中包含以应用程序名称命名的用户下载目录(实际上,应用程序名称文件夹只是指向以Package Family Name命名的文件夹的链接)为了获取文件夹名称,我使用下面的hack(vb)首先在UWP应用程序的下载文件夹中创建一个虚拟文件,然后使用.NET代码获取目录名称,最后删除虚拟文件。

Dim o As StorageFile = Await DownloadsFolder.CreateFileAsync("dummy.txt", CreationCollisionOption.GenerateUniqueName)
Dim dirName Ss String = Path.GetDirectoryName(o.Path)
Await o.DeleteAsync(StorageDeleteOption.PermanentDelete)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM