I have an application that provides the user the ability to backup the local SQLite database to another location. I then provide the ability to restore the backup database and overwrite the current database with the backup copy. In my restore command, I make a backup of the database file, which seems redundant. I feel like this code could be simplified.
public async void RestoreDatabase()
{
bool success;
var pickedFile = default(StorageFile);
var picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".sqlite");
picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
var file = await picker.PickSingleFileAsync();
if (file != null)
{
var pickedFileToken = StorageApplicationPermissions.FutureAccessList.Add(file);
_settings.RestoreFileToken = pickedFileToken;
pickedFile = file;
}
// first make a backup of the current database file
var currentDatabase = await _currentFolder.GetFileAsync("MyMoney.sqlite");
if (currentDatabase != null)
{
currentDatabase.CopyAsync(ApplicationData.Current.RoamingFolder, "MyMoney.bak",
NameCollisionOption.ReplaceExisting);
}
try
{
if (pickedFile != null)
{
await pickedFile.CopyAsync(ApplicationData.Current.RoamingFolder, "MyMoney.sqlite",
NameCollisionOption.ReplaceExisting);
success = true;
}
else
{
success = false;
}
}
catch (Exception)
{
success = false;
}
if (!success)
{
// restore the backup file
var backupDatabase = await _currentFolder.GetFileAsync("MyMoney.bak");
if (backupDatabase != null)
{
await backupDatabase.CopyAsync(ApplicationData.Current.RoamingFolder, "MyMoney.sqlite",
NameCollisionOption.ReplaceExisting);
}
}
}