How to deploy a file in UWP

南笙酒味 提交于 2019-12-06 15:46:26

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.

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