如何在 UWP 中部署文件 [英] How to deploy a file in UWP
问题描述
我的 UWP 需要加载一个文件,我想知道:
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?
当我使用 VS 2015 部署我的应用程序时,如何将此文件部署到我想要的文件夹?
How to deploy this file to the folder I want when I deploy my app using VS 2015?
推荐答案
您可以将文件作为资源或内容文件添加到您的项目中.如果文件类似于图像、SQLite 数据库或文本文件,最好的方法是 Content
构建操作.
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.
然后您可以使用 StorageFile.GetFromApplicationUriAsync
访问该文件:
You can then access the file using StorageFile.GetFromApplicationUriAsync
:
var storeLogoFile = StorageFile.GetFromApplicationUriAsync(
new Uri( "ms-appx:///Assets/StoreLogo.png" ) );
如果您喜欢使用 System.IO
,也可以获取 Path
本身:
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
然而,这种方法仅在您只需要读取文件时才有效.如果您还需要修改它,则必须先将其复制到ApplicationData.Current.LocalFolder
:
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 );
您可能只想执行一次此操作,因此您可以先检查该文件是否已存在于 ApplicationData
中,然后再继续.
You might want to do this only once, so you can first check if the file already exists in ApplicationData
before proceeding.
这篇关于如何在 UWP 中部署文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!