반응형
How to import the db file in xamarin to the project and copy the file to Android when starting the program and use it
Xamarin => xxx.Android Project =>MainActivity.cs => OnCreate Method => LoadApplication(new App) Bottom Add.
string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
string dbPath = Path.Combine(path, "test.db");
CopoyToDevice(dbPath);
Copy Method!
/// <summary>
/// Copy To Device sqlite db File
/// </summary>
/// <param name="dbPath">Local Path</param>
private void CopoyToDevice(string dbPath)
{
bool flag = true;
#if DEBUG == false
flag = !File.Exists(dbPath)
#endif
if (flag)
{
using (var br = new BinaryReader(Application.Context.Assets.Open("test.db")))
{
using (var bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
{
byte[] buffer = new byte[2048];
int length = 0;
while ((length = br.Read(buffer, 0, buffer.Length)) > 0)
{
bw.Write(buffer, 0, length);
}
}
}
}
}
In debug mode, copying is always attempted, and in release mode, when there is no file, copying is attempted only once.
Used!
반응형
'[개발] 이야기 > [DotNet] 이야기' 카테고리의 다른 글
xamarin floating button 추가하기 (0) | 2022.04.12 |
---|---|
xamarin 페이지 이동 시 파라미터 넘기기 (0) | 2022.04.12 |
xamarin android admob 광고 붙이기 - 삽질여정기 - 2 (0) | 2022.04.08 |
xamarin android admob 광고 붙이기 - 2일간 삽질의 결과물 (삽질했던 내용도 공유!) - 1 (0) | 2022.04.08 |
efcore left join 하는 방법 (include 시 nullable) (0) | 2022.03.02 |
댓글