Tag Archives: GetFilesAsync

GetFilesAsync is not returning all the files in the “My Documents” folder

The following code should return a list of all files in the “My Documents” folder:

    var files = await KnownFolders.DocumentsLibrary.GetFilesAsync();
    List<FileInformation> filesList = new List<FileInformation>();
    System.Diagnostics.Debug.WriteLine($"File count: {files.Count()}");

The returned count from this code is (in my case) 11. However, excluding folders, there are 61 files in that directory for me. When I iterate through the collection, it does indeed find 11.

After scratching my head for a while, I finally realised that the answer to my question was my own blog post. It only lists the allowed types.

UWP Accessing Documents Library

Accessing the documents library from a UWP app is frowned upon by Microsoft; however, it is possible. Here is some code that will access the library:

var files = await KnownFolders.DocumentsLibrary.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName);
foreach (var f in files)
    BasicProperties props = await f.GetBasicPropertiesAsync();

This will access the library and get the properties for each file. However, just running it will fail with this error:


So, you’ll probably get this error and, like me (and not for the first time), go looking for it here:


Of course, you won’t find it (because it’s not there), and then you’ll turn to Google. If that brought you here then you’re next step is to open the manifest file directly:

    <uap:Capability Name="documentsLibrary" />

If there are already Capabilities then just add the line:

    <uap:Capability Name="documentsLibrary" />

Note: you need the uap prefix.

And, that’s not all. Next you need to tell it which documents it can access:

        <uap:Extension Category="windows.fileTypeAssociation">
          <uap:FileTypeAssociation Name=".txt">
    <uap:Capability Name="documentsLibrary" />

And that’s it. I can understand why they have all these restrictions, but they can be frustrating for programmers.

Generating a Temporary Filename in Windows 10 Universal App

It appears that the functionality to create a temporary file was omitted from Windows 8 / 10

If anyone knows this to not be the case then please add a comment to that effect.

Consequently, I decided to create a little helper method:

        public static async Task<StorageFile> CreateTempFile(
                 StorageFolder folder, string extension)
            string fileName = string.Empty;

            while (true)
                // Get a random filename
                fileName = string.Format("{0}.{1}", 
                     Guid.NewGuid().ToString(), extension);

                // Check if it already exists
                IReadOnlyList<StorageFile> fileList =
                    await folder.GetFilesAsync();

                // If it does then loop until we have a unique one
                if (!fileList.Any(f => f.DisplayName == fileName)) break;

            var newFile = await folder.CreateFileAsync(fileName);
            return newFile;