Beispiel #1
0
        private void BackupButton_Click(object sender, RoutedEventArgs e)
        {
            // Don't run if worker is already busy
            if (Worker.IsBusy)
            {
                return;
            }

            String backupLocation = BackupLocationBox.Text + "\\";

            StatusText.Content = "Running backup...";

            if (Settings.DateType == DateType.Readable)
            {
                backupLocation += DateTime.Now.ToString("yyyy-M-d THH-mm-ss");
            }
            else
            {
                backupLocation += DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            }

            BackupButton.IsEnabled = false;

            BackupArgs workerArgs = new BackupArgs(SaveLocationBox.Text, backupLocation);

            Worker.RunWorkerAsync(workerArgs);
        }
Beispiel #2
0
        private void RunBackup(Object sender, DoWorkEventArgs e)
        {
            BackupArgs       args    = (BackupArgs)e.Argument;
            String           status  = "";
            bool             success = false;
            BackgroundWorker worker  = (BackgroundWorker)sender;

            try
            {
                worker.ReportProgress(0, "Copying directories: ");

                // Copy dir (thanks https://stackoverflow.com/a/3822913)
                string[] directories       = Directory.GetDirectories(args.SaveLocation, "*", SearchOption.AllDirectories);
                int      directoryCount    = directories.Length;
                int      directoriesCopied = 0;

                foreach (string dirPath in directories)
                {
                    Directory.CreateDirectory(dirPath.Replace(args.SaveLocation, args.BackupLocation));

                    directoriesCopied++;

                    worker.ReportProgress(0, "Copying directories (" + directoriesCopied + " / " + directoryCount + ")");
                }

                string[] files       = Directory.GetFiles(args.SaveLocation, "*.*", SearchOption.AllDirectories);
                int      fileCount   = files.Length;
                int      filesCopied = 0;

                foreach (string newPath in files)
                {
                    File.Copy(newPath, newPath.Replace(args.SaveLocation, args.BackupLocation), true);

                    filesCopied++;

                    worker.ReportProgress(0, "Copying files (" + filesCopied + " / " + fileCount + ")");
                }

                success = true;
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);

                status = "Error: " + exception.Message;
            }

            if (success)
            {
                status = "Backup complete.";
            }

            e.Result = status;

            return;
        }