简体   繁体   English

SpecialFolder。个人现有的sqlite数据库

[英]SpecialFolder.Personal existing sqlite database

On my Xamarin android application i create db path using the "System.Environment.SpecialFolder.Personal". 在我的Xamarin android应用程序上,我使用“ System.Environment.SpecialFolder.Personal”创建数据库路径。 I have an existing sqlite database. 我有一个现有的sqlite数据库。 Where in the project and with what properties must i include the database to be used in the peronal folder? 我在项目中的何处以及必须具有哪些属性的Peronal文件夹中包含要使用的数据库?

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 您可以使用Xamarin.Essentials来访问只读的包/资产资源并复制它,而不必使用本机平台代码,或者如果您在Xamarin.Android项目中进行编码,则可以直接使用Android.Content的资产

So assuming you have a database in your Android Assets folder: 因此,假设您的Android Assets文件夹中有一个数据库:

Assets
   db.sqlite

Xamarin Essentials / NetStd2.0 Code Example: 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 code sample (Directly use Assets ): 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM