I've been trying to add a backup/restore option to my app, so I searched and found some useful info (that seemed easy to do).
One of them being about the File
from Xamarin.Essentials
, which I tried to use, but keeps giving me an "access denied" error.
Getting the db directory:
public Restaurar()
{
InitializeComponent();
localPath = Path.Combine(FileSystem.AppDataDirectory, templateFileName);
}
The original file and the backup names and also the localpath
:
const string templateFileName = "database.sqlite";
const string localFileName = "backup_database.sqlite";
string localPath;
The function to create a backup:
private void BtnBackup_Clicked(object sender, EventArgs e)
{
var reading = File.ReadAllText(localPath);
File.WriteAllText(localFileName, reading);
DisplayAlert("BACKUP", "Backup file created!", "OK");
}
Moving the backup file to the specific database folder:
private void BtnMove_Clicked(object sender, EventArgs e)
{
File.Move(localFileName, FileSystem.AppDataDirectory);
}
I got the following exception when clicking the Backup button:
System.UnauthorizedAccessException: 'Access to the path "/backup_database.sqlite" is denied.'
And clicking the Move button:
System.IO.FileNotFoundException: 'Could not find file'/backup_database.sqlite'.'
I thought that would create a new file on the project folder which I would be able to move the android/IOS database folder. I appreciate any help, thanks in advance!
EDIT:
I tried a different approach:
I already have a DependecyService to get the platform specific path to the database, so I used it.
Backup:
private void BtnBackup_Clicked(object sender, EventArgs e)
{
var dep = DependencyService.Get<IPath>();
string db_path = dep.GetPath("database.sqlite");
string backup_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
var origin = File.ReadAllBytes(db_path);
File.WriteAllBytes(backup_path, origin);
DisplayAlert("BACKUP", "Backup created!", "OK");
}
Restore:
private void BtnRestaurar_Clicked(object sender, EventArgs e)
{
var dep = DependencyService.Get<IPath>();
string db_path = dep.GetPath("database.sqlite");
string backup_path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
var origin = File.ReadAllBytes(backup_path);
File.WriteAllBytes(db_path, origin);
DisplayAlert("RESTAURING", "Backup restored!", "OK");
}
I'm getting the following exception:
System.UnauthorizedAccessException: 'Access to the path '/data/user/0/com.companyname.VendasEstoque/files/.local/share' is denied.'