Search code examples
iosxamarinresourcesxamarin-studioresourcedictionary

How to access a list of all files in resource folder of my project?


I have some .TXT files saved in Resource folder of my project. I want to display a list to user in which all files from resource folder are shown and user can select the file he desires.

Later on i will read the user selected file and show it on screen.


Solution

  • This gets FileInfo's on all txt files in the resources:

            var fileInfos = NSBundle.GetPathsForResources(".txt", path)
                .Select(a => new FileInfo(a));
    

    Now you have the short name, full name etc to play with:

            foreach (var fileInfo in fileInfos)
            {
                System.Diagnostics.Debug.WriteLine(fileInfo.Name);
    
                using (var streamReader = new StreamReader(new FileStream(fileInfo.FullName, FileMode.Open)))
                {
                    System.Diagnostics.Debug.WriteLine(streamReader.ReadToEnd());
                }
            }