//GetDatabase private void GetDatabase() { try { if (thread_getdatabase != null && thread_getdatabase.ThreadState == ThreadState.Running) { } else { thread_getdatabase = new Thread(() => { try { this.dtgDatabase.Dispatcher.Invoke((Action)(() => { dtgDatabase.Visibility = System.Windows.Visibility.Hidden; dtgDatabase.ItemsSource = null; })); this.mpr.Dispatcher.Invoke((Action)(() => { this.mpr.Visibility = System.Windows.Visibility.Visible; this.mpr.IsActive = true; })); list_tb_database.Clear(); //if database type is sqlite if (StaticClass.GeneralClass.flag_database_type_general == false) { //create folder when not exist if (!System.IO.Directory.Exists(current_directory + @"\DBRES_Local")) { System.IO.Directory.CreateDirectory(current_directory + @"\DBRES_Local"); } directory = System.IO.Directory.GetDirectories(current_directory + @"\DBRES_Local"); } // if database type is sqlserver else { //create folder when not exist if (!System.IO.Directory.Exists(current_directory + @"\DBRESSer_Local")) { System.IO.Directory.CreateDirectory(current_directory + @"\DBRESSer_Local"); } directory = System.IO.Directory.GetDirectories(current_directory + @"\DBRESSer_Local"); } string directory_name = ""; string _directory_name = ""; string __directory_name = ""; int id = 0; bool flag = false; for (int i = 0; i < directory.Length; i++) { flag = false; id++; directory_name = System.IO.Path.GetFileName(directory[i]); _directory_name = directory_name.Replace("..", ":"); __directory_name = _directory_name.Replace(".", "/"); //if database type is sqlite if (StaticClass.GeneralClass.flag_database_type_general == false) { //check exist for database in folder if (System.IO.File.Exists(directory[i] + @"\CheckOut.db") == true) { file_info = new System.IO.FileInfo(directory[i] + @"\CheckOut.db"); if (file_info.Length == 0) { System.IO.Directory.Delete(directory[i], true); flag = true; } } else { System.IO.Directory.Delete(directory[i], true); flag = true; } } //if database type is sqlserver else { //check exist for database in folder if (System.IO.File.Exists(directory[i] + @"\CheckOut.sql") == true) { file_info = new System.IO.FileInfo(directory[i] + @"\CheckOut.sql"); if (file_info.Length == 0) { System.IO.Directory.Delete(directory[i], true); flag = true; } } else if (System.IO.File.Exists(directory[i] + @"\" + CashierRegisterDAL.ConnectionDB.getSqlServerDataBaseName() + ".bak") == true) { file_info = new System.IO.FileInfo(directory[i] + @"\" + CashierRegisterDAL.ConnectionDB.getSqlServerDataBaseName() + ".bak"); if (file_info.Length == 0) { System.IO.Directory.Delete(directory[i], true); flag = true; } } else { System.IO.Directory.Delete(directory[i], true); flag = true; } } if (flag == false) { EC_tb_Database ec_tb_database = new EC_tb_Database(); ec_tb_database.Id = id; ec_tb_database.BackupDate = __directory_name; //get file info //if database type is sqlite if (StaticClass.GeneralClass.flag_database_type_general == false) { file_info = new System.IO.FileInfo(directory[i] + @"\CheckOut.db"); } //if database type is sqlserver else { if (System.IO.File.Exists(directory[i] + @"\" + CashierRegisterDAL.ConnectionDB.getSqlServerDataBaseName() + ".bak") == true) { file_info = new System.IO.FileInfo(directory[i] + @"\" + CashierRegisterDAL.ConnectionDB.getSqlServerDataBaseName() + ".bak"); } else { file_info = new System.IO.FileInfo(directory[i] + @"\CheckOut.sql"); } } ec_tb_database.FileSize = (file_info.Length / 1000).ToString() + "KB"; ec_tb_database.BitmapImage_Delete = bitmapimage_delete; ec_tb_database.BitmapImage_Restore = bitmapimage_restore; list_tb_database.Add(ec_tb_database); } } Thread.Sleep(500); this.mpr.Dispatcher.Invoke((Action)(() => { this.mpr.Visibility = System.Windows.Visibility.Hidden; this.mpr.IsActive = false; })); this.dtgDatabase.Dispatcher.Invoke((Action)(() => { dtgDatabase.Visibility = System.Windows.Visibility.Visible; dtgDatabase.ItemsSource = list_tb_database; })); this.tblTotal.Dispatcher.Invoke((Action)(() => { this.tblTotal.Text = FindResource("total").ToString() + "(" + list_tb_database.Count.ToString() + ")"; })); } catch (Exception) { this.Dispatcher.Invoke((Action)(() => { ModernDialog md = new ModernDialog(); md.Title = FindResource("notification").ToString(); md.Content = FindResource("have_not_access").ToString(); md.ShowDialog(); })); } }); thread_getdatabase.Start(); } } catch (Exception ex) { this.Dispatcher.Invoke((Action)(() => { ModernDialog md = new ModernDialog(); md.Title = FindResource("notification").ToString(); md.Content = ex.Message; md.ShowDialog(); })); } }
//GetDatabase private void GetDatabase() { try { if (thread_getdatabase != null && thread_getdatabase.ThreadState == ThreadState.Running) { } else { thread_getdatabase = new Thread(() => { try { this.mpr.Dispatcher.Invoke((Action)(() => { mpr.IsActive = true; dtgDatabase.ItemsSource = null; })); list_tb_database.Clear(); //UserCredential user_credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = client_id, ClientSecret = client_secret, }, new[] { DriveService.Scope.Drive }, "user", CancellationToken.None, new FileDataStore("TuanNguyen.GoogleDrive.Auth.Store")).Result; UserCredential user_credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = GeneralClass.client_id, ClientSecret = GeneralClass.client_secret, }, new[] { DriveService.Scope.DriveFile }, "user", CancellationToken.None, new FileDataStore("TuanNguyen.GoogleDrive.Auth.Store")).Result; //create the drive service var driveservice = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = user_credential, ApplicationName = "Get database backup" }); this.Dispatcher.Invoke((Action)(() => { this.muiBtnBackup.Visibility = System.Windows.Visibility.Visible; })); //get file from gdrive string str_condition = "Backup_CashierRegister_"; try { FileList file_list = driveservice.Files.List().Execute(); string client_folderid_lc = ""; string client_folderid_gd = ""; string client_foldername_gd = ""; //if database type is sqlite if (StaticClass.GeneralClass.flag_database_type_general == false) { client_foldername_gd = "CashierRegister_Backup"; } else { client_foldername_gd = "CashierRegister_Ser_Backup"; } //if ClientFolderInfo file isn't existed if (!System.IO.File.Exists("ClientFolderInfo")) { bool flag_exist_client_folderid = false; for (int i = 0; i < file_list.Items.Count; i++) { //if local ClientFolderInfo file is't exist and CashierRegister_Backup is existed if ((file_list.Items[i].Title == client_foldername_gd) && (file_list.Items[i].MimeType == "application/vnd.google-apps.folder") && (file_list.Items[i].ExplicitlyTrashed == false)) { //create ClientFolderInfo file System.IO.StreamWriter streamwriter = new System.IO.StreamWriter("ClientFolder"); //if database type is sqlite if (StaticClass.GeneralClass.flag_database_type_general == false) { streamwriter.WriteLine("FolderID:" + file_list.Items[i].Id.ToString()); streamwriter.WriteLine("FolderID:"); } //if database tupe is sqlserver else { streamwriter.WriteLine("FolderID:"); streamwriter.WriteLine("FolderID:" + file_list.Items[i].Id.ToString()); } streamwriter.Close(); StaticClass.GeneralClass.EncryptFileGD("ClientFolder", "ClientFolderInfo", StaticClass.GeneralClass.key_register_general); client_folderid_lc = file_list.Items[i].Id; client_folderid_gd = file_list.Items[i].Id; flag_exist_client_folderid = true; break; } } //if local ClientFolderInfo file isn't existed and drive ClientFolderInfo isn't existed if (flag_exist_client_folderid == false) { File folder_client = new File(); //if database type is sqlite if (StaticClass.GeneralClass.flag_database_type_general == false) { folder_client.Title = "CashierRegister_Backup"; } //if database type is sqlserver else { folder_client.Title = "CashierRegister_Ser_Backup"; } folder_client.Description = "This folder using for backup database"; folder_client.MimeType = "application/vnd.google-apps.folder"; //insert folder File response_folder = driveservice.Files.Insert(folder_client).Execute(); if (response_folder != null) { System.IO.StreamWriter streamwriter = new System.IO.StreamWriter("ClientFolder"); //if database type is sqlite if (StaticClass.GeneralClass.flag_database_type_general == false) { streamwriter.WriteLine("FolderID:" + response_folder.Id.ToString()); streamwriter.WriteLine("FolderID:"); } //if database tupe is sqlserver else { streamwriter.WriteLine("FolderID:"); streamwriter.WriteLine("FolderID:" + response_folder.Id.ToString()); } streamwriter.Close(); StaticClass.GeneralClass.EncryptFileGD("ClientFolder", "ClientFolderInfo", StaticClass.GeneralClass.key_register_general); client_folderid_gd = response_folder.Id; client_folderid_lc = response_folder.Id; } } } //if local ClientFolderInfo file is existed else { bool flag_exist_client_folderid = false; System.IO.StreamReader streamreader_folder = StaticClass.GeneralClass.DecryptFileGD("ClientFolderInfo", StaticClass.GeneralClass.key_register_general); if (streamreader_folder != null) { //if database type is sqlite if (StaticClass.GeneralClass.flag_database_type_general == false) { client_folderid_lc = streamreader_folder.ReadLine().Split(':')[1]; } //if database type is sqlserver else { streamreader_folder.ReadLine(); client_folderid_lc = streamreader_folder.ReadLine().Split(':')[1]; } streamreader_folder.Close(); } //if client_folderid_lc isn't null if (!String.IsNullOrWhiteSpace(client_folderid_lc)) { for (int i = 0; i < file_list.Items.Count; i++) { //if local ClientFolderInfo file is exist and drive ClientFolderInfo is existed if ((file_list.Items[i].MimeType == "application/vnd.google-apps.folder") && (file_list.Items[i].ExplicitlyTrashed == false) && (file_list.Items[i].Id == client_folderid_lc)) { client_folderid_gd = file_list.Items[i].Id; flag_exist_client_folderid = true; break; } } } else { for (int i = 0; i < file_list.Items.Count; i++) { if ((file_list.Items[i].Title == client_foldername_gd) && (file_list.Items[i].MimeType == "application/vnd.google-apps.folder") && (file_list.Items[i].ExplicitlyTrashed == false)) { client_folderid_gd = file_list.Items[i].Id; flag_exist_client_folderid = true; System.IO.StreamWriter streamwriter = new System.IO.StreamWriter("ClientFolder"); //get FolderID System.IO.StreamReader stream_reader_temp = StaticClass.GeneralClass.DecryptFileGD(current_directory + @"\ClientFolderInfo", StaticClass.GeneralClass.key_register_general); //if database type is sqlite if (StaticClass.GeneralClass.flag_database_type_general == false) { streamwriter.WriteLine("FolderID:" + file_list.Items[i].Id); stream_reader_temp.ReadLine(); streamwriter.WriteLine("FolderID:" + stream_reader_temp.ReadLine().Split(':')[1].ToString()); } //if database tupe is sqlserver else { streamwriter.WriteLine("FolderID:" + stream_reader_temp.ReadLine().Split(':')[1].ToString()); streamwriter.WriteLine("FolderID:" + file_list.Items[i].Id); } //close stream_reader_temp if (stream_reader_temp != null) { stream_reader_temp.Close(); } streamwriter.Close(); StaticClass.GeneralClass.EncryptFileGD("ClientFolder", "ClientFolderInfo", StaticClass.GeneralClass.key_register_general); client_folderid_gd = file_list.Items[i].Id; client_folderid_lc = file_list.Items[i].Id; break; } } } //if local ClientFolderInfo file is existed and drive ClientFolderInfo isn't existed if (flag_exist_client_folderid == false) { File folder_client = new File(); //if database type is sqlite if (StaticClass.GeneralClass.flag_database_type_general == false) { folder_client.Title = "CashierRegister_Backup"; } //if database type is sqlserver else { folder_client.Title = "CashierRegister_Ser_Backup"; } folder_client.Description = "This folder using for backup database"; folder_client.MimeType = "application/vnd.google-apps.folder"; //insert folder File response_folder = driveservice.Files.Insert(folder_client).Execute(); if (response_folder != null) { System.IO.StreamWriter streamwriter = new System.IO.StreamWriter("ClientFolder"); //get FolderID System.IO.StreamReader stream_reader_temp = StaticClass.GeneralClass.DecryptFileGD(current_directory + @"\ClientFolderInfo", StaticClass.GeneralClass.key_register_general); //if database type is sqlite if (StaticClass.GeneralClass.flag_database_type_general == false) { streamwriter.WriteLine("FolderID:" + response_folder.Id.ToString()); stream_reader_temp.ReadLine(); streamwriter.WriteLine("FolderID:" + stream_reader_temp.ReadLine().Split(':')[1].ToString()); } //if database tupe is sqlserver else { streamwriter.WriteLine("FolderID:" + stream_reader_temp.ReadLine().Split(':')[1].ToString()); streamwriter.WriteLine("FolderID:" + response_folder.Id.ToString()); } //close stream_reader_temp if (stream_reader_temp != null) { stream_reader_temp.Close(); } streamwriter.Close(); StaticClass.GeneralClass.EncryptFileGD("ClientFolder", "ClientFolderInfo", StaticClass.GeneralClass.key_register_general); client_folderid_gd = response_folder.Id; client_folderid_lc = response_folder.Id; } } } int no = 0; for (int i = 0; i < file_list.Items.Count; i++) { if (file_list.Items[i].Title.Length > 23) { if (file_list.Items[i].Parents.Count > 0) { if ((file_list.Items[i].Parents[0].Id == client_folderid_lc) && (file_list.Items[i].MimeType == "application/octet-stream") && (file_list.Items[i].Title.Substring(0, 23) == str_condition) && (file_list.Items[i].ExplicitlyTrashed == false)) { EC_tb_Database ec_tb_database = new EC_tb_Database(); ec_tb_database.Id = ++no; ec_tb_database.IdDatabase = file_list.Items[i].Id.ToString(); ec_tb_database.DownloadUrl = file_list.Items[i].DownloadUrl; ec_tb_database.BackupDate = file_list.Items[i].CreatedDate.ToString(); ec_tb_database.FileSize = (file_list.Items[i].FileSize / 1000).ToString() + "KB"; ec_tb_database.BitmapImage_Restore = bitmapimage_restore; ec_tb_database.BitmapImage_Delete = bitmapimage_delete; _lstExt.Add(file_list.Items[i].FileExtension.ToString()); list_tb_database.Add(ec_tb_database); } } } } } catch (System.Net.Http.HttpRequestException) { this.Dispatcher.Invoke((Action)(() => { ModernDialog md = new ModernDialog(); md.Title = FindResource("notification").ToString(); md.Content = FindResource("internet_problem").ToString(); md.CloseButton.Content = FindResource("close").ToString(); md.ShowDialog(); })); } this.Dispatcher.Invoke((Action)(() => { tblTotal.Text = FindResource("checkout").ToString() + "(" + list_tb_database.Count.ToString() + ")"; dtgDatabase.ItemsSource = list_tb_database; mpr.IsActive = false; dtgDatabase.Visibility = System.Windows.Visibility.Visible; })); //get email address string email_address = driveservice.About.Get().Execute().User.EmailAddress.ToString(); this.Dispatcher.Invoke((Action)(() => { this.tblEmailAddress.Visibility = System.Windows.Visibility.Visible; this.tblEmailAddress.Text = driveservice.About.Get().Execute().User.EmailAddress; //set login logout this.tblLogout.Visibility = System.Windows.Visibility.Visible; this.tblLogin.Visibility = System.Windows.Visibility.Hidden; })); } catch (AggregateException) { this.Dispatcher.Invoke((Action)(() => { ModernDialog md = new ModernDialog(); md.Title = FindResource("notification").ToString(); md.Content = FindResource("have_not_access").ToString(); md.ShowDialog(); })); } }); thread_getdatabase.Start(); } } catch (Exception ex) { this.Dispatcher.Invoke((Action)(() => { ModernDialog md = new ModernDialog(); FindResource("notification").ToString(); md.Content = ex.Message; md.ShowDialog(); })); } }