C# 将文件从UWP资源复制到本地文件夹
我正在尝试将一个文件从UWP资源复制到用户的本地文件夹中 我能得到的最接近的结果是:C# 将文件从UWP资源复制到本地文件夹,c#,uwp,C#,Uwp,我正在尝试将一个文件从UWP资源复制到用户的本地文件夹中 我能得到的最接近的结果是: public static void CopyDatabaseIfNotExists(string dbPath) { var storageFile = IsolatedStorageFile.GetUserStoreForApplication(); if (storageFile.FileExists(dbPath)) { retu
public static void CopyDatabaseIfNotExists(string dbPath)
{
var storageFile = IsolatedStorageFile.GetUserStoreForApplication();
if (storageFile.FileExists(dbPath))
{
return;
}
using (var resourceStream = Application.GetResourceStream(new Uri("preinstalledDB.db", UriKind.Relative)).Stream)
{
using (var fileStream = storageFile.CreateFile(dbPath))
{
byte[] readBuffer = new byte[4096];
int bytes = -1;
while ((bytes = resourceStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
fileStream.Write(readBuffer, 0, bytes);
}
}
}
}
但这似乎不再适用于UWP。
GetResourceStream不再可用(“应用程序不包含“GetResourceStream”的定义”)
谁能告诉我如何使用UWP来实现这一点
多谢各位 您可以简单一点,只需将
ApplicationData.Current.LocalFolder
替换为所需的文件夹即可
try
{
await ApplicationData.Current.LocalFolder.GetFileAsync("preinstalledDB.db");
// No exception means it exists
return;
}
catch (System.IO.FileNotFoundException)
{
// The file obviously doesn't exist
}
// Cant await inside catch, but this works anyway
var storfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///preinstalledDB.db"));
await storfile.CopyAsync(ApplicationData.Current.LocalFolder);
try
块可能看起来很奇怪,但实际上它是确定文件是否存在的最快方法。谢谢,但是我的void是公共静态void CopyDatabaseIfNotExists(string dbPath)。它不允许等待操作员。有没有一种方法可以使用wait操作符呢?还有,wait是异步的,对吗?这就是说,空白将立即返回,对吗?我不确定这是否真的是我需要的。您是否可以分享一些关于如何使用代码的见解?是的,您需要使方法异步并等待它。没办法。但这种方法只有在我尝试打开数据库后才有可能成功,对吧???旁注:因为C#6,所以可以在catch中等待。