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 DoBackupBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { DoBackupBackgroundWorker_Arguments workerArgs = (DoBackupBackgroundWorker_Arguments)e.Argument; BackupFiles(workerArgs.WorkingFileList, workerArgs.WorkingDirectory, workerArgs.WorkingName); }