private void MainForm_DragDrop(object sender, DragEventArgs e) { string[] droppedFiles = (string[])e.Data.GetData(DataFormats.FileDrop); if (droppedFiles == null) { return; } string firstItem = droppedFiles[0]; string workingFileList = Path.GetTempFileName(); _TempFile = workingFileList; string workingDirectory = Path.GetDirectoryName(firstItem); string workingName = null; FileAttributes firstItem_Attributes = File.GetAttributes(firstItem); StringBuilder fileList = new StringBuilder(); foreach (string file in droppedFiles) { fileList.AppendLine(file); } File.WriteAllText(workingFileList, fileList.ToString(), Encoding.UTF8); // Example Path: C:\TopDirectory\SubDirectory\SomeFile.txt if (droppedFiles.Length == 1) // If there is only one dropped file or directory. { if (firstItem_Attributes.HasFlag(FileAttributes.Directory)) // If it is a directory. // Returns: SubDirectory { workingName = new DirectoryInfo(firstItem).Name; } else // If it is a file. // Returns: SomeFile { workingName = Path.GetFileNameWithoutExtension(firstItem); } } else // If there is more than one dropped file or directory. // If the first one in the list is a directory. { if (firstItem_Attributes.HasFlag(FileAttributes.Directory)) { // Returns: TopDirectory workingName = new DirectoryInfo(Path.GetDirectoryName(firstItem)).Name; } else // If the first one in the list is a file. // Returns: SubDirectory { workingName = new DirectoryInfo(Path.GetDirectoryName(firstItem)).Name; } } if (string.IsNullOrWhiteSpace(workingName)) { MessageBox.Show("workingName is blank or null!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Dispose(); } #if DEBUG Debug.WriteLine(workingName); Debug.WriteLine(workingDirectory); Debug.WriteLine(workingFileList); #endif DoBackupBackgroundWorker_Arguments workerArgs = new DoBackupBackgroundWorker_Arguments() { WorkingFileList = workingFileList, WorkingDirectory = workingDirectory, WorkingName = workingName }; BackupWorkingProgressBar.BringToFront(); TaskbarProgress.SetState(Handle, TaskbarProgress.TaskbarStates.Indeterminate); DoBackupBackgroundWorker.RunWorkerAsync(workerArgs); }
private void BackupFiles(string workingFileList, string workingDirectory, string workingName) { string sevenZip = Settings.CurrentSettings.Backup.SevenZip; string sevenZipMethods = Settings.CurrentSettings.Backup.SevenZipMethods; // M-d-yyyy string dateDate = DateTime.Now.ToString(Settings.CurrentSettings.Backup.DateFormat).ToLower(); // h.mmtt string dateTime = DateTime.Now.ToString(Settings.CurrentSettings.Backup.TimeFormat).ToLower(); string tempFileName = Settings.CurrentSettings.Backup.NameFormat; tempFileName = tempFileName.Replace("%F", workingName); tempFileName = tempFileName.Replace("%D", dateDate); tempFileName = tempFileName.Replace("%T", dateTime); workingName = Path.Combine(workingDirectory, tempFileName); if (File.Exists(workingName)) { MessageBox.Show($"There is a file where the backup zip is trying to be saved with the same name.\r\n\r\n{workingName}\r\n\r\nBackup will exit when you click OK.", "Backup zip already exists", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } string sevenZipArguments = "a \"" + workingName + "\" " + $"@\"{workingFileList}\" {sevenZipMethods}"; #if DEBUG Debug.WriteLine(dateTime); Debug.WriteLine(sevenZipArguments); Debug.WriteLine("===="); #endif try { if (!File.Exists(sevenZip)) { throw new FileNotFoundException("7-Zip was not found on the system!", sevenZip); } Process sevenZipProcess = new Process { StartInfo = new ProcessStartInfo { FileName = sevenZip, Arguments = sevenZipArguments, WindowStyle = ProcessWindowStyle.Hidden } }; if (Settings.CurrentSettings.Arguments.Elevate == true) { sevenZipProcess.StartInfo.Verb = "runas"; } sevenZipProcess.Start(); sevenZipProcess.WaitForExit(); } catch (FileNotFoundException e) { MessageBox.Show($"{e.Message}\r\n\r\n7z.exe was expected at: {sevenZip}", "7-Zip not found", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } catch (Exception e) { MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } finally { DoBackupBackgroundWorker.ReportProgress(100); try { File.Delete(_TempFile); } catch { } if (File.Exists(workingName)) { long fileSize = new FileInfo(workingName).Length; string fileSizeFormatted = BytesToString(fileSize); if (Settings.CurrentSettings.Arguments.DoAutocopy) { try { string tempWorkingName = new FileInfo(workingName).Name; string tempWorkingNames = ""; foreach (string copyLocation in Settings.CurrentSettings.Backup.CopyTo) { string copyTo = Path.Combine(copyLocation, tempWorkingName); tempWorkingNames += copyTo + "\r\n"; File.Copy(workingName, copyTo, false); } MessageBox.Show($"Zip file saved to: {workingName}\r\n(size {fileSizeFormatted})\r\n\r\nZip file was also copied to:\r\n\r\n{tempWorkingNames}", "Finished", MessageBoxButtons.OK, MessageBoxIcon.Information); Application.Exit(); } catch (Exception e) { MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } } else { MessageBox.Show($"Zip file saved to: {workingName}\r\n(size {fileSizeFormatted})", "Finished", MessageBoxButtons.OK, MessageBoxIcon.Information); Application.Exit(); } } else { MessageBox.Show("The zip file was not found after saving!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } } }