I have a solution to this issue for Winforms. You can configure it out whatever you like.
We need to backup the SQLite database file before your application has been updated and then We'll restore it at the first start up after update.
To prevent that restore SQLite database file on every start up we will create a setting in application. Follow these
- Right click your main class
- Click Properties
- Click Settings and create new setting like Name=updateDB, Type=bool, Scope=User, Value=True
Now we have created that check for update DB setting. After this we will call to BackupDatabase() function immediately after await Manager.UpdateApp();
private static void BackupDatabase()
{
// Get the current Database File
string dbFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "database.db");
// Set the temporary destination for backup file
string destination = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\..\\database.db";
// Backup current DB file to destination
File.Copy(dbFile, destination, true);
}
private async void btnUpdateApply_Click(object sender, EventArgs e)
{
await manager.UpdateApp();
BackupDatabase();
}
We have successfully backup db file. Now we should check updateDB's value to execute restore process in Program.cs. If the value is true then restore the old db file to new version of your application.
private static void RestoreDB()
{
//Restore database after application update
string dest = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\database.db";
string sourceFile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\..\\database.db";
// Check if we have settings that we need to restore
if (!File.Exists(sourceFile))
// Nothing we need to do
return;
// Create directory as needed
try
{
Directory.CreateDirectory(Path.GetDirectoryName(dest));
}
catch (Exception) { }
// Copy our backup file in place
try
{
File.Copy(sourceFile, dest, true);
}
catch (Exception) { }
// Delete backup file
try
{
File.Delete(sourceFile);
}
catch (Exception) { }
}
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (Settings.Default.updateDB)
{
RestoreDB();
Settings.Default.updateDB = false;
Settings.Default.Save();
Settings.Default.Reload();
}
Aplication.Run(new Form1());
}