2

このコード行に問題があります。デバイスに sqlite データベースを作成したい。

 string dbPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\test.db";
        if (!System.IO.File.Exists(dbPath))

            using (System.IO.Stream sr = ***Assets***.Open("test.db"))
            {
                using (System.IO.Stream srTo = System.IO.File.Create(dbPath))
                {
                    sr.CopyTo(srTo);
                }
            }

このメッセージは次のことを示します: The name 'Assets' does not exist in the current context

同様のプロジェクトがありますが、大きなものよりも包括的です。エラーはありません。私が定義に従っているアセットは、c ドライブの cs ファイルを定義し、Android.Content.ContextWrapper への参照を提供します。プロジェクトのパスにありません。【アプリにファイルを追加した方法は?

4

2 に答える 2

3

アクティビティでこれを行っていない場合は、アクティビティ/コンテキストへの参照が必要です。これをコンストラクターのヘルパー クラスに渡す必要があります。

public yourClass(Activity context.......){

      context.Assets.Open("your.db");
}
于 2013-04-11T14:29:29.053 に答える
0

次のようにできます。

using (System.IO.Stream sr = Android.App.Application.Context.Assets.Open("test.db"))
{
     using (System.IO.Stream srTo = System.IO.File.Create(dbPath))
     {
          sr.CopyTo(srTo);
     }
}
于 2019-07-08T06:55:04.957 に答える