UWP - Get path to user download folder
Asked Answered
R

4

4

I have been looking for a little while now and am not finding much help via MSDN resources and others.

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.

How do I get the current user's Download folder path in a Windows Universal App?

Rutile answered 10/10, 2016 at 0:15 Comment(1)
DownloadsFolder is not suiting my needs so what are you looking for precisely?Encaenia
N
2

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

Ninon answered 11/10, 2016 at 7:11 Comment(2)
I figured this was the solution but wasn't sure if I was overlooking a framework or class in NETCore that would already be provided. Thank you!Rutile
This is a static, bad practice solution. It relies on the filesystem not changing, this is more elegant: UserDataPaths.GetDefault().DownloadsMana
F
14

Use Windows.Storage.UserDataPaths to get the path of user's download folder.

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.
  • You shouldn't obtain downloads folder path using LocalFolder, which might result in wrong folder when the user changed the default location for it.
Furl answered 23/12, 2018 at 11:29 Comment(1)
This is more reliable answer!Tetramethyldiarsine
U
4
System.Environment.ExpandEnvironmentVariables("%userprofile%/downloads/")
Ubald answered 9/7, 2018 at 15:7 Comment(0)
N
2

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

Ninon answered 11/10, 2016 at 7:11 Comment(2)
I figured this was the solution but wasn't sure if I was overlooking a framework or class in NETCore that would already be provided. Thank you!Rutile
This is a static, bad practice solution. It relies on the filesystem not changing, this is more elegant: UserDataPaths.GetDefault().DownloadsMana
S
0

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.

Dim o As StorageFile = Await DownloadsFolder.CreateFileAsync("dummy.txt", CreationCollisionOption.GenerateUniqueName)
Dim dirName Ss String = Path.GetDirectoryName(o.Path)
Await o.DeleteAsync(StorageDeleteOption.PermanentDelete)
Societal answered 11/2, 2020 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.