1

In my PC, I've changed my default downloads folder from "C:\Users\MyProfile\Downloads" to "D:\Downloads".

Now, in my UWP app, I am able to save the files that user downloads from my app in the "D:\Downloads" using the Windows.Storage API DownloadsFolder class. But I need to display the path, where the files are getting downloaded, to the user.

I'm not able to fetch the location which the user has set as his default download location in the case as I have said above (from c: drive to d: drive).

Is there any way to check if the user has set a different location for his downloads and if so get the folder path?

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

The above code always gives "C:\Users\MyProfile\Downloads". But I've changed my download location to D: drive.

Sethu Bala
  • 457
  • 3
  • 17
  • no there isnt a way to check that so far. – Muhammad Touseef Sep 27 '18 at 10:18
  • https://stackoverflow.com/questions/3795023/how-to-programmatically-derive-windows-downloads-folder-userprofile-downloads – Dave Sep 27 '18 at 10:19
  • Possible duplicate of [How to programmatically derive windows downloads folder "%USERPROFILE%/Downloads"?](https://stackoverflow.com/questions/3795023/how-to-programmatically-derive-windows-downloads-folder-userprofile-downloads) – Dave Sep 27 '18 at 10:20
  • Can you not just tell them "it's in your downloads folder"? If they've moved it, they should know where they've moved it to. If they haven't, they should still be able to find it easily (after all, it's a shortcut in the left pane of explorer windows) – Damien_The_Unbeliever Sep 27 '18 at 10:23
  • @Damien_The_Unbeliever In UWP, a folder will be created inside the downloads folder with my app name, and files will be stored inside this app folder. So it will be better if I display the path. If not there is a chance that the user might be searching for the files directly in the downloads folder instead of searching for a folder with my app's name inside his downloads folder. – Sethu Bala Sep 27 '18 at 10:26
  • 1
    Cannot try it now, but my first thought is create a temp file in the Downloads folder and then check the [Path property](https://learn.microsoft.com/en-us/uwp/api/windows.storage.storagefile.path#Windows_Storage_StorageFile_Path) of the file. – kennyzx Sep 27 '18 at 11:29
  • @kennyzx ya I thought of that approach, but since it would unnecessarily create a temp file, I wanted to check if there is any direct way to get the downloads folder path. – Sethu Bala Sep 27 '18 at 12:10

1 Answers1

1

You can not get the DownloadsFolder's path that the user set on the device directly. You can only get the download folder's path by the StorageFile or StorageFolder's Path property where you created them in the DownloadsFolder.

You can do a trick to get the DownloadsFolder's path by the steps: Create a file in the DownloadsFolder => Get the file's path and save the path => Delete the file.

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";
    }
}
Breeze Liu - MSFT
  • 3,734
  • 1
  • 10
  • 13
  • 1
    I don't get it. Where is StorageFile and why do you need that when you already have a ``DownloadsFolder`` class... I can't find any of these objects... – Grisgram Apr 02 '19 at 10:25