1

My UWP needs to load a file and I want to know:

  1. Where to put this file, in the package installation folder or the application data folder?

  2. How to deploy this file to the folder I want when I deploy my app using VS 2015?

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
EnJ
  • 175
  • 1
  • 9
  • If you want to add a file that will be shipped with the application, add it to your solution, set its build action as content, then you can find it in the [*InstalledLocation* or by URI](https://stackoverflow.com/q/23924424/2681948). *AplicationData* folder is used for the data that you add after your app has been installed. – Romasz Feb 25 '18 at 08:41

1 Answers1

2

You can add files either as resources or content files to your project. If the file is something like a Image, SQLite Database or text file, the best approach will be the Content build action.

You can then access the file using StorageFile.GetFromApplicationUriAsync:

var storeLogoFile = StorageFile.GetFromApplicationUriAsync( 
        new Uri( "ms-appx:///Assets/StoreLogo.png" ) );

You can also get the Path itself if your prefer to use System.IO:

var packagePath = Package.Current.InstalledLocation;
var filePath = Path.Combine( packagePath, "Assets/StoreLogo.png" );
//do something with the file

This approach however works only if you only need to read the file. If you also need to modify it, you will have to copy it to ApplicationData.Current.LocalFolder first:

var applicationDataFileCopy = 
     storeLogoFile.CopyAsync( ApplicationData.Current.LocalFolder );

You might want to do this only once, so you can first check if the file already exists in ApplicationData before proceeding.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • Thanks, Martin. It is a binary file. I do not need to write it, just read it. Is it OK to put it at package installation folder? – EnJ Feb 25 '18 at 20:40
  • If it solved your problem, please consider accepting the answer as the solution so that the question is resolved :-) – Martin Zikmund Feb 25 '18 at 20:41
  • If you just want to read it, it is ok to put it in the project as a `Content` file and read it from the package installation folder. – Martin Zikmund Feb 25 '18 at 20:45