Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将文件从Visual Studio项目复制到UWP应用程序&x27;正在生成本地文件夹_C#_Xamarin_Uwp - Fatal编程技术网

C# 如何将文件从Visual Studio项目复制到UWP应用程序&x27;正在生成本地文件夹

C# 如何将文件从Visual Studio项目复制到UWP应用程序&x27;正在生成本地文件夹,c#,xamarin,uwp,C#,Xamarin,Uwp,我有一个Xamarin解决方案,里面有一个UWP项目。我无法理解如何将VS项目中的文件复制到UWP LocalFolder。我尝试过摆弄文件的构建操作(编译、内容、嵌入资源、附加文件),并尝试手动创建此位置 显然,这个代码: Windows.Storage.StorageFolder appDataFolder = Windows.Storage.ApplicationData.Current.LocalFolder; string fileName = (appDataFolder.Path

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

显然,这个代码:

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\u 9tywq191txc1p\LocalState\HighScore.csv'


因此,我只需要弄清楚如何将项目中的CSV文件获取到此LocalState文件夹。非常感谢您的帮助。

您可以输入一些代码将其复制到那里:

// 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);

这是最重要的。希望它对您有用。

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

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

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

它将被复制到您的
软件包中。InstallLocation
,如果您需要在运行时修改文件,则需要手动复制到
本地文件夹中。非常好,谢谢。没想到会这么复杂和隐蔽。欢迎来到UWP!:P