SpecialFolder.Personal 现有的sqlite 数据库 [英] SpecialFolder.Personal existing sqlite database

查看:32
本文介绍了SpecialFolder.Personal 现有的sqlite 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Xamarin android 应用程序上,我使用System.Environment.SpecialFolder.Personal"创建了 db 路径.我有一个现有的 sqlite 数据库.我必须在项目的哪个位置以及哪些属性中包含要在 peronal 文件夹中使用的数据库?

On my Xamarin android application i create db path using the "System.Environment.SpecialFolder.Personal". I have an existing sqlite database. Where in the project and with what properties must i include the database to be used in the peronal folder?

推荐答案

您可以使用 Xamarin.Essentials 访问您的只读捆绑包/资产资源并复制它,而无需使用本机平台代码或者,如果您在 Xamarin.Android 项目中编码,则可以直接使用 Android.Content 的资产

You can use Xamarin.Essentials to access your read-only bundle/asset resource and copy it without have to use native platform code or if you are coding within an Xamarin.Android project, you can directly use Android.Content's Assets

假设您的 Android Assets 文件夹中有一个数据库:

So assuming you have a database in your Android Assets folder:

Assets
   db.sqlite

Xamarin Essentials/NetStd2.0 代码示例:

var copyToLocationPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "db.sqlite");
if (!File.Exists(copyToLocationPath))
{
    using (var assetStream = await Xamarin.Essentials.FileSystem.OpenAppPackageFileAsync("db.sqlite"))
    using (var fileStream = new FileStream(copyToLocationPath, FileMode.Create, FileAccess.Write))
    {
        await assetStream.CopyToAsync(fileStream);
    }
}
var connection = new SqliteConnection(copyToLocationPath);

Xamarin Android 代码示例(直接使用Assets):

var copyToLocationPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "db.sqlite");
if (!File.Exists(copyToLocationPath))
{
    using (var assetStream = Assets.Open("db.sqlite"))
    using (var fileStream = new FileStream(copyToLocationPath, FileMode.Create, FileAccess.Write))
    {
         await assetStream.CopyToAsync(fileStream);
    }
}

这篇关于SpecialFolder.Personal 现有的sqlite 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆