[英]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.