如何在构建时将文件从 Visual Studio 项目复制到 UWP 应用程序的 LocalFolder [英] How to copy a file from Visual Studio project to UWP application's LocalFolder on build

查看:32
本文介绍了如何在构建时将文件从 Visual Studio 项目复制到 UWP 应用程序的 LocalFolder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Xamarin 解决方案,其中包含一个 UWP 项目.我无法理解如何将 VS 项目中的文件复制到 UWP LocalFolder.我尝试摆弄文件的构建操作(编译、内容、嵌入资源、附加文件),并尝试手动创建此位置.

I have a Xamarin solution with a UWP project in it. I cannot understand how to copy a file that is in my VS project to the UWP LocalFolder. I've tried fiddling with the file's Build Action (Compile, Content, Embedded Resource, AdditionalFiles) and I've tried to manually create this location.

显然是这段代码:

Windows.Storage.StorageFolder appDataFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

string fileName = (appDataFolder.Path + "\\HighScore.csv");

创建此路径:

'C:\Users\<我的名字>\AppData\Local\Packages\ebf29fcf-0080-4b4c-b873-78fd1340811d_9tywq191txc1p\LocalState\HighScore.csv'

所以我只需要弄清楚如何将我项目中的 CSV 文件放到这个 LocalState 文件夹中.任何帮助表示赞赏.

So I just need to figure out how to get the CSV file that's in my project to this LocalState folder. Any help appreciated.

推荐答案

首先,将 .csv 文件添加到您的资产文件夹并将构建操作设置为 Content.

Firstly, add the .csv file to your assets folder and set the build action to Content.

之后,您可以使用以下代码段在运行时手动将文件从 Assets 复制到本地文件夹.

After that, you can manually copy the file from Assets to your Local Folder at runtime with the following snippet.

var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/HighScore.csv"));
 if (file != null)
 {
     // Copy .csv file to LocalFolder so we can read / write to it.
     // No CollisionOption will default to Fail if file already exists,
     // to copy every time the code is run, add NameCollisionOption.ReplaceExisting
     await file.CopyAsync(ApplicationData.Current.LocalFolder);

     // Get path of newly created file
     String newFile = ApplicationData.Current.LocalFolder.Path + "/HighScore.csv";
 }

这篇关于如何在构建时将文件从 Visual Studio 项目复制到 UWP 应用程序的 LocalFolder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆