1

In my UWP application I am trying to count and list all the files in a directory. The code looks like the following.

private async void SetUpDemo()
{
    StorageFolder appInstalledFolder = Package.Current.InstalledLocation;
    StorageFolder assets = await appInstalledFolder.GetFolderAsync(@"Assets\AppTestStuff");
    IReadOnlyList<StorageFile> files = await assets.GetFilesAsync();

    Debug.WriteLine("File Count: " + files.Count);

    foreach(StorageFile file in files)
    {
        Debug.WriteLine(file.Name);
    }
}

And Assets\AppTestStuff looks like the following.

enter image description here

However, the output from this code is the following.

File Count: 2

2017-12-04 20.38.29-2.jpg

Test Text Doc.txt

I was expecting a output that looked a bit more like the following.

File Count: 6

[The 6 files from the photo]

What is causing this discrepancy and how can it be resolved?

Community
  • 1
  • 1
Dan
  • 7,286
  • 6
  • 49
  • 114
  • Have you tried using one of the overloads [GetFilesAsync(CommonFileQuery)](https://msdn.microsoft.com/es-xl/library/br227274.aspx) | [GetFilesAsync(CommonFileQuery, UInt32, UInt32)](https://msdn.microsoft.com/es-xl/library/br227275.aspx)? – Ivan García Topete Jul 12 '18 at 19:04
  • According to [this answer](https://stackoverflow.com/a/27746412/9453080), using `GetFiilesAsync(CommonFileQuery.OrderByName)` can fetch all the files in a folder – Ivan García Topete Jul 12 '18 at 19:13
  • @IvanGarcíaTopete Thanks for the comments. I have tried that query. My issue is that it is not finding all top level files, I'm not that interested in any sub directories – Dan Jul 12 '18 at 19:43
  • What are you trying to achieve with those files? Are the files being added by you, or by the user at runtime? – Ivan García Topete Jul 12 '18 at 19:48

1 Answers1

3

Your code looks fine. My crystal ball suggests that it is providing correct results: only the .jpg and .txt file are packaged and deployed with the app. The others aren't reported since they aren't there. You can confirm this by looking in the app's staging directory, probably: %yourprojectdir%\bin\*%Architecture%\%Configuration%*\AppX\Assets\AppTestStuff

Make sure that the data files that you want installed with the project have the Build Action: Content property. In Visual Studio's Solution Explorer select the file, right click and choose "Properties" (or have the Properties window open), and look for the Build Action.

Properties dialog for helloworld.pdf with Build Action Content in a hand-drawn red circle

Text and image files will default to Content, but the other types you have will default to Build Action None. Switch those to Content and they'll copy on over.

Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54