How to get all sub folders and its files - UWP

喜你入骨 提交于 2019-12-20 03:23:18

问题


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?


回答1:


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..




回答2:


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.



来源:https://stackoverflow.com/questions/47344292/how-to-get-all-sub-folders-and-its-files-uwp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!