[英]Copy file from UWP resources to local folder
I'm trying to copy a file from the UWP resources into the user's local folder. 我正在尝试将文件从UWP资源复制到用户的本地文件夹中。
The closest I could get was this: 我能得到的最接近的是:
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);
}
}
}
}
But this doesn't seem to work anymore with UWP. 但这似乎不适用于UWP。 GetResourceStream isn't available anymore ("Application doesn't contain a definition for 'GetResourceStream'").
GetResourceStream不再可用(“应用程序不包含'GetResourceStream'的定义”)。
Could somebody please tell me how do this with UWP? 有人可以告诉我如何使用UWP吗?
Thank you very much! 非常感谢你!
You can do it a bit simpler, just replace ApplicationData.Current.LocalFolder
with your desired folder. 您可以将其简化一些,只需将
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);
The try
block may seem strange but it is actually the fastest way to determine if a file exists. try
块可能看起来很奇怪,但这实际上是确定文件是否存在的最快方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.