How to get all sub folders and its files - UWP

前端 未结 2 1592
清歌不尽
清歌不尽 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 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..

提交回复
热议问题