获取用户为其 PC 设置的默认下载位置

Get default download location set by the user for his PC

在我的 PC 中,我已将默认下载文件夹从 "C:\Users\MyProfile\Downloads" 更改为 "D:\Downloads"。

现在,在我的 UWP 应用程序中,我可以使用 Windows.Storage API DownloadsFolder class 将用户从我的应用程序下载的文件保存在 "D:\Downloads" 中。但是我需要向用户显示下载文件的路径。

在我上面所说的情况下,我无法获取用户设置为默认下载位置的位置(从 c: 驱动器到 d: 驱动器)。

有没有办法检查用户是否为他的下载设置了不同的位置,如果是的话获取文件夹路径?

Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\Downloads")

上面的代码总是给出"C:\Users\MyProfile\Downloads"。不过我已经把下载位置改成D:盘了

您无法直接获取用户在设备上设置的DownloadsFolder路径。您只能通过在 DownloadsFolder.

中创建它们的 StorageFileStorageFolderPath 属性 获取下载文件夹的路径

您可以通过以下步骤获取 DownloadsFolder 的路径:在 DownloadsFolder 中创建文件 => 获取文件路径并保存路径 => 删除文件.

private async Task<string> GetDownloadsFolderPath()
{
    StorageFile newFile = await DownloadsFolder.CreateFileAsync("mytestfile");
    if (newFile != null)
    {
        //You maybe need to operate the DownloadFolderPath string to subtract the folder name of your app.
        string DownloadFolderPath = newFile.Path;
        await newFile.DeleteAsync();
        return DownloadFolderPath;
    }
    else
    {
        return "There is an error to get path";
    }
}