How to get all sub folders and its files - UWP

前端 未结 2 1591
清歌不尽
清歌不尽 2021-01-23 00:59

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 correspondi

相关标签:
2条回答
  • 2021-01-23 01:12

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

    0 讨论(0)
  • 2021-01-23 01:13

    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.

    0 讨论(0)
提交回复
热议问题