Ajay G
Ajay G

Reputation: 43

UWP : How to get a list of all the text file names from a sub folder of Documents folder

Learning to develop for windows 10. I'm trying to access a Sub-folder inside the documents folder and list all the text file names that are present in the subfolder. Please help.

Upvotes: 0

Views: 149

Answers (1)

Faywang - MSFT
Faywang - MSFT

Reputation: 5868

If you want to access Documents folder, you can use FolderPicker or KnownFolders.DocumentsLibrary method.

About FolderPicker, you need to access folders by interacting with a picker, but you can directly choose the folder you want to. About KnownFolders.DocumentsLibrary method, you need to add extra broadFileSystemAccess capability and allow your app to access file system in settings. ​

//FolderPicker
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
folderPicker.FileTypeFilter.Add("*");
Windows.Storage.StorageFolder subFolder = await folderPicker.PickSingleFolderAsync();

//KnownFolders.DocumentsLibrary
//StorageFolder Myfolder = KnownFolders.DocumentsLibrary;
//StorageFolder subFolder = await Myfolder.GetFolderAsync("YouSubFolder");
​
List<string> fileTypeFilter = new List<string>();
fileTypeFilter.Add(".txt");
QueryOptions queryOptions = new QueryOptions(Windows.Storage.Search.CommonFileQuery.OrderByName, fileTypeFilter);
StorageFileQueryResult queryResult = subFolder.CreateFileQueryWithOptions(queryOptions);
var files = await queryResult.GetFilesAsync();
foreach (var file in files)
{
    string name = file.Name;
}

Upvotes: 1

Related Questions