2

I am making a Windows 10 UWP App. As part of this app I need to be able to search inside the User Device's Downloads folder (Not the Apps Downloads folder). I have created a Folder Picker for the user to be able to choose the downloads folder themselves. However, I need to do this without the user. Here is My Folder Picker:

FolderPicker picker = new FolderPicker();
picker.FileTypeFilter.Add("*");
picker.ViewMode = PickerViewMode.List;
picker.SuggestedStartLocation = PickerLocationId.Downloads;
StorageFolder folder = await picker.PickSingleFolderAsync();

Is there any way in which I could use something like Folder Picker, but hard-coded so the destination is always set to one place (The Downloads Folder)?

rook
  • 5,880
  • 4
  • 39
  • 51
James Tordoff
  • 661
  • 1
  • 5
  • 25
  • You need to have a privilege to that folder, you can progarmmatically use KnownFolders, otherwise user will have to grant access to your app by picker. I'm not sure if you will be able to bypass this with official API. – Romasz May 10 '16 at 21:00
  • Agree with @Romasz, the [DownloadsFolder](https://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.storage.downloadsfolder.aspx) does not privde method to search the file in DownloadsFolder. It has create file methods. – Jayden May 11 '16 at 02:03
  • @JaydenGu Yes I have come across the limitation. I was planning to let users save their manuals to downloads and then check if they existed to save them downloading them again. I did not want to save manuals within my App Sandbox as they will also want to view them externally of the app. Back to the drawing board and to find another approach. Thank you both for the confirmation it is appreciated. – James Tordoff May 11 '16 at 09:25
  • @JamesTordoff Just to add - once the user selects folder by a picker once, then you can access it even programmatically - no need to use picker again. – Romasz May 11 '16 at 17:13
  • @Romasz I didn't know that, I learnt something new. Unfortunately however i need to not use the picker whatsoever. I am sure what you have told me will come to good use in the future so thanks! Do you know if the same restrictions apply to folders such as documents as they do the Downloads folder? – James Tordoff May 12 '16 at 09:03
  • I'm not sure about documents folder - some time ago it was restricted to special apps, but I'm not sure how it's now. Also remember that those folders are virtual locations = for example Music folder can exist on sd/memory, your file can be writted, depending on device/user settings. – Romasz May 12 '16 at 09:24

1 Answers1

1

You are not allowed to search the Downloads folder, but if all you want to do is regain access to a file you previously downloaded, you can use the FutureAccessList.

using Windows.Storage.AccessCache;

file = await DownloadsFolder.CreateFileAsync(...);
var token = StorageApplicationPermissions.FutureAccessList.Add(file,
                                           "anything you like goes here");

You probably want to save the token in your app's local storage so you won't forget it.

Use the token to regain access to the file in the future.

file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token);
Raymond Chen
  • 44,448
  • 11
  • 96
  • 135