How to deploy a file in UWP

假装没事ソ 提交于 2020-01-03 03:13:10

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/48970802/how-to-deploy-a-file-in-uwp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!