2
votes

I have a fixed folder in desktop called student_names. it contains sub folders and each sub folders contains its files. Now I want to store those sub folders and its corresponding file names in an array list. how is it possible?

2
Have you tried anything, just to give you the idea you need to recursively traverse in subfolders, a little google can help you. - Ipsit Gaur
Tried with StorageFolder. But cant fix it path to Desktop/student_names - user2431727
Ohh I skipped seeing that you are accessing Desktop folder, that would not be possible in UWP, UWP works in a sandbox model. - Ipsit Gaur

2 Answers

3
votes

UWP Applications cannot directly access a folder (except application folders) without the user explicitly providing access to the folder (atleast once).

So in order to always be able to read/write to a specific folder you need to first ask user to select the specific folder. Once the user selects a folder you may save it to FutureAccessList which will provide you with a token to directly get access to the folder(without user intervention) for future use..

 var picker = new FolderPicker();
 picker.FileTypeFilter.Add("*");
 var folder = await picker.PickSingleFolderAsync();
 //add selected folder to FutureAccessList
StorageApplicationPermissions.FutureAccessList.AddOrReplace("futureAccessToken", folder);

From next launch (or any logic that you implement) you can access the folder and list its contents

 var folder = await StorageApplicationPermissions.FutureAccessList.
                 GetFolderAsync("futureAccessToken");
 IReadOnlyList<StorageFile> fileList = await folder.GetFilesAsync();

So, you just need to provide access to the parent folder (which in your case is student_names) then you will be able to access its sub folders and files.

Hope this helps..

1
votes

Unfortunately, that may not be possible. It's been a while since I last worked on UWP apps, but it was not possible to access file system outside a few designated folders (e.g., app folder and a few other special folders like Document, Music, Picture, etc., if your app declares proper permissions). You cannot access folders on Desktop or C:\ folder in general.