using MongoDB.Driver; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using System.IO.Compression; namespace BuechermarktClient { public class Backup { public static void MakeBackup() { string folder = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); string backupfolder = folder + "\\backups"; if (!Directory.Exists(backupfolder)){ Directory.CreateDirectory(backupfolder); } string tbackupfoldername = "" + (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; string tbackupfolder = backupfolder + "\\" + tbackupfoldername; Directory.CreateDirectory(tbackupfolder); var bt = MainWindow.BookTypeCollection.Find(e => true).ToList(); var json = JsonConvert.SerializeObject(bt); StringToFile(tbackupfolder + "\\BookTypes.json", json); var b = MainWindow.BookCollection.Find(e => true).ToList(); json = JsonConvert.SerializeObject(b); StringToFile(tbackupfolder + "\\Book.json", json); var s = MainWindow.StudentCollection.Find(e => true).ToList(); json = JsonConvert.SerializeObject(s); StringToFile(tbackupfolder + "\\Students.json", json); ZipFile.CreateFromDirectory(tbackupfolder, tbackupfolder + ".zip"); DeleteDirectory(tbackupfolder); } private static void StringToFile(string filepath, string json) { //File.Create(filepath); File.WriteAllText(filepath, json); } public static void DeleteDirectory(string target_dir) { string[] files = Directory.GetFiles(target_dir); string[] dirs = Directory.GetDirectories(target_dir); foreach (string file in files) { File.SetAttributes(file, FileAttributes.Normal); File.Delete(file); } foreach (string dir in dirs) { DeleteDirectory(dir); } Directory.Delete(target_dir, false); } } }