简体   繁体   English

如何在构建时将文件从Visual Studio项目复制到UWP应用程序的LocalFolder

[英]How to copy a file from Visual Studio project to UWP application's LocalFolder on build

I have a Xamarin solution with a UWP project in it. 我有一个带有UWP项目的Xamarin解决方案。 I cannot understand how to copy a file that is in my VS project to the UWP LocalFolder. 我不明白如何将VS项目中的文件复制到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. 我尝试摆弄文件的Build Action(编译,内容,嵌入式资源,AdditionalFiles),并尝试手动创建此位置。

Apparently this code: 显然这段代码:

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

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

Creates this path: 创建此路径:

'C:\\Users\\<my Name>\\AppData\\Local\\Packages\\ebf29fcf-0080-4b4c-b873-78fd1340811d_9tywq191txc1p\\LocalState\\HighScore.csv'

So I just need to figure out how to get the CSV file that's in my project to this LocalState folder. 因此,我只需要弄清楚如何将项目中的CSV文件获取到此LocalState文件夹。 Any help appreciated. 任何帮助表示赞赏。

You could put in some code to copy it there: 您可以输入一些代码以将其复制到此处:

// This would point to (in respect to your build platform target):
// C:\Users\User\Source\Repos\MyAwesomeApp\MyAwesomeApp.UWP\bin\x64\Debug\AppX
Uri uri = new Uri("ms-appx:///HighScores.csv");


// throws Exception if file doesn't exist
var file = await StorageFile.GetFileFromApplicationUriAsync(uri);


// copy the file to the package:
await file.CopyAsync(ApplicationData.Current.LocalFolder);

That's the jist of it. 这就是关键。 Hope it works for you. 希望对你有效。

Firstly, add the .csv file to your assets folder and set the build action to Content . 首先,将.csv文件添加到资产文件夹,并将构建操作设置为Content

内容

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";
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Visual Studio中生成项目的设置设计器文件 - How to generate a project's settings designer file on build in Visual Studio Visual Studio 2015 WinForms项目中的“无法复制文件”生成错误 - “Unable to copy file” build error in Visual Studio 2015 WinForms project 在UWP的LocalFolder中创建一个文件夹并将文件复制到该文件夹 - Create a folder in LocalFolder of UWP and copy files to it 如何使用 Visual Studio 调试 Xamarin UWP 项目 - How to debug Xamarin UWP project with Visual Studio 从项目文件中构建不带Microsoft Visual Studio的可执行文件 - Build an executable file without Microsoft Visual Studio from project file 从LocalFolder保存文件 - Saving a file from LocalFolder uwp项目无法在Visual Studio 2017 15.8上构建 - uwp project not able to build on Visual Studio 2017 15.8 由于缺少引用,无法使用Visual Studio 2017构建UWP项目 - Failed to build UWP project with Visual Studio 2017 due to missing reference 如何从C#项目的构建事件中访问Visual Studio解决方案级平台? - How can you access the Visual Studio solution level platform from a C# project's build event? Visual Studio,部署项目和文件副本 - Visual Studio, Deployment project and file copy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM