I have a Win8 app that needs to use a Sqlite DB; every time that the app starts i check that file exists, if not exists i try to copy it.
The problem is that when i copy it the app tries to open it before the copy ended and i get an error:
I call the following function and then try to query the database:
public async static void CopyDatabase()
{
bool isExisting = false;
try
{
StorageFile storage = await ApplicationData.Current.LocalFolder.GetFileAsync("dbname.db");
isExisting = true;
}
catch (Exception ex)
{
isExisting = false;
}
if (!isExisting)
{
StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync("dbname.db");
await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
}
}
How can i know when the copy is ended? What am i doing wrong?
I don’t think that the issue is in this method’s code, but in how you call it.
Change your method’s declaration to:
And call it using:
This will guarantee that the copy is completed before your next line of code (which probably tries to open the database) is executed.