backup and restore of sqlite database from c# -sqlserver
class Program
{
private static readonly string filePath = Environment.CurrentDirectory;
static void Main(string[] args)
{
var filename = "bazarganidb.db";
var bkupFilename = Path.GetFileNameWithoutExtension(filename) + ".bak";
CreateDB(filePath, filename);
BackupDB(filePath, filename, bkupFilename);
RestoreDB(filePath, bkupFilename, filename, true);
}
private static void RestoreDB(string filePath, string srcFilename, string
destFileName, bool IsCopy = false)
{
var srcfile = Path.Combine(filePath, srcFilename);
var destfile = Path.Combine(filePath, destFileName);
if (File.Exists(destfile)) File.Delete(destfile);
if (IsCopy)
BackupDB(filePath, srcFilename, destFileName);
else
File.Move(srcfile, destfile);
}
private static void BackupDB(string filePath, string srcFilename, string
destFileName)
{
var srcfile = Path.Combine(filePath, srcFilename);
var destfile = Path.Combine(filePath, destFileName);
if (File.Exists(destfile)) File.Delete(destfile);
File.Copy(srcfile, destfile);
}
private static void CreateDB(string filePath, string filename)
{
var fullfile = Path.Combine(filePath, filename);
if (File.Exists(fullfile)) File.Delete(fullfile);
File.WriteAllText(fullfile, "this is the dummy data");
}
}