My UWP needs to load a file and I want to know:
Where to put this file, in the package installation folder or the application data folder?
How to deploy this file to the folder I want when I deploy my app using VS 2015?
My UWP needs to load a file and I want to know:
Where to put this file, in the package installation folder or the application data folder?
How to deploy this file to the folder I want when I deploy my app using VS 2015?
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.