0
votes

When using Visual Studio 2017 to build UWP applications any Asset files that I mark with a Build Action of "Content" and set "Copy Always" for the "Copy to Output directory" property are getting copied to the projects appropriate \Bin ..\Debug project folder.

But those files are not getting copied over to the runtime Local\Packages .. \LocalState folder and therefore are not available to the app if I try to read them from the ApplicationData.Current.LocalFolder.

Is this a bug in Visual Studio or with my dev environment? Or is this by design? If it is by design, what is the recommended way of distributing a folder full of assets or data files with your app that you can enumerate through at runtime?

In the past, I have used the

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new System.Uri("ms-appx:///assets/xxx.png")); 

syntax when needing access to a specific file or two, but am looking for a way to deploy a folder of files that could contain 1-to-many files, depending on the purpose of the build and would rather not have to hardcode a long list of file uri's into the app for each file, and then alter it when changing out the files for a special build.

2

2 Answers

0
votes

Copy Always are not copied to LocalState folder.

Maybe you misunderstand the function about 'Copy to Output directory', it will not be output to the local folder.

If you want to copy the files to the localFolder, you need to manually copy. About how to manually copy, the files which have set "Copy Always" will be generated in the installation directory, you can go to the corresponding folder to find the files and use CopyAsync() method to copy to the local folder.

0
votes

After further digging I found what I was looking for.

From an API point of view, it appears that the location that the "Copy to Output directory" puts files is considered the InstalledLocation of the Package. So instead of trying to enumerate the files of the LocalState folder, or its subfolders, it is necessary to retrieve the InstalledLocation folder and enumerate it in order to access Asset files distributed with the application.

For example:

StorageFolder installedFolder =  Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder assetsFolder = await installedFolder.GetFolderAsync("assets");
IReadOnlyList<StorageFile> pictures = await assetsFolder.GetFilesAsync();

would be the way to access the Assets folder or any other subfolder of Assets, or the root of your project if you adjust the GetFolderAsync parameter accordingly.