Ejemplo n.º 1
0
        private void DoBackup(BackgroundWorker bgWorker, DoWorkEventArgs e)
        {
            BackupDataArgs backupData = e.Argument as BackupDataArgs;

            int currentProgress = 0;

            bgWorker.ReportProgress(currentProgress);

            //zip progressvalue is calculated different from copy progressvalue
            int progressValue = (backupData.FileSettings[(int)FSettings.ZipFile]) ? 40 / (backupData.BackupFiles.Count) : 80 / (backupData.BackupFiles.Count * backupData.BackupLocations.Count);

            //add this number every time a file is copied, not that accurate but w/e

            //shows that everything works and is ready to copy
            currentProgress = 20;

            #region copy files

            //if the files are being copied into a subfolder or the given folder
            if (backupData.FileSettings[(int)FSettings.SubFolder] || backupData.FileSettings[(int)FSettings.GivenDirectory])
            {
                foreach (string f in backupData.BackupFiles) //copy each file to every backup location, f=file, l=location
                {
                    foreach (string l in backupData.BackupLocations)
                    {
                        try
                        {
                            if (backupData.FileSettings[(int)FSettings.GivenDirectory])  //copy files in given directory
                            {
                                File.Copy(f, Path.Combine(l, Path.GetFileName(f)), backupData.FileSettings[(int)FSettings.Override]);
                            }

                            else if (backupData.FileSettings[(int)FSettings.SubFolder])
                            {
                                if (backupData.FileSettings[(int)FSettings.AddDateToFileName])    //create subfolder and add date to foldername
                                {
                                    File.Copy(f, Path.Combine(l, backupData.FileName + "_" + backupData.DTNow.Day + "-" + backupData.DTNow.Month + "-" + backupData.DTNow.Year, Path.GetFileName(f)), backupData.FileSettings[(int)FSettings.Override]);
                                }
                                else                        //just create a subfolder
                                {
                                    File.Copy(f, Path.Combine(l, backupData.FileName, Path.GetFileName(f)), backupData.FileSettings[(int)FSettings.Override]);
                                }
                            }

                            currentProgress = (currentProgress + progressValue < 100) ? currentProgress + progressValue : 100; //100 is maximum, if it would go above just set it 100
                            bgWorker.ReportProgress(currentProgress);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                            e.Cancel = true;
                            return;
                        }
                    }
                }
            }

            #endregion

            #region files in zip

            else //make a zip file
            {
                if (!Directory.Exists(backupData.FileName)) //create temporary folder to make zip
                {
                    Directory.CreateDirectory(backupData.FileName);
                }

                foreach (string f in backupData.BackupFiles)
                {
                    File.Copy(f, Path.Combine(backupData.FileName, Path.GetFileName(f)), true);
                    currentProgress = (currentProgress + progressValue < 60) ? currentProgress + progressValue : 60; //60 is maximum for this operation, if it goes above just set it to 60
                    bgWorker.ReportProgress(currentProgress);
                }

                //adjust progressvalue to locations, 35% progress for locations
                progressValue = 35 / backupData.BackupLocations.Count;

                foreach (string l in backupData.BackupLocations)          //copy zip to every location
                {
                    if (backupData.FileSettings[(int)FSettings.Override]) //if overriding is true, delete old identical zip files
                    {
                        try
                        {
                            if (File.Exists(Path.Combine(l, backupData.FileName + "_" + backupData.DTNow.Day + "-" + backupData.DTNow.Month + "-" + backupData.DTNow.Year + ".zip")))
                            {
                                File.Delete(Path.Combine(l, backupData.FileName + "_" + backupData.DTNow.Day + "-" + backupData.DTNow.Month + "-" + backupData.DTNow.Year + ".zip"));
                            }

                            if (File.Exists(Path.Combine(l, backupData.FileName + ".zip")))
                            {
                                File.Delete(Path.Combine(l, backupData.FileName + ".zip"));
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                            e.Cancel = true;
                            return;
                        }
                    }

                    try
                    {
                        if (checkBoxDate.Checked)
                        {
                            ZipFile.CreateFromDirectory(backupData.FileName, Path.Combine(l, backupData.FileName + "_" + backupData.DTNow.Day + "-" + backupData.DTNow.Month + "-" + backupData.DTNow.Year + ".zip"));
                        }
                        else
                        {
                            ZipFile.CreateFromDirectory(backupData.FileName, Path.Combine(l, backupData.FileName + ".zip"));
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                        e.Cancel = true;
                        return;
                    }

                    currentProgress = (currentProgress + progressValue < 95) ? currentProgress + progressValue : 95; //95 is maximum for this operation
                    bgWorker.ReportProgress(currentProgress);
                }

                bgWorker.ReportProgress(95);
                Directory.Delete(backupData.FileName, true);
            }

            #endregion
        }
Ejemplo n.º 2
0
        private void buttonOK_Click(object sender, EventArgs e)
        {
            DateTime      dt = DateTime.Now; //for adding date to file names
            List <string> backupLocations = new List <string>();
            List <string> backupFiles     = new List <string>();

            //taking values from checkboxes and radiobuttons
            bool[] fileSettings = new bool[] { checkBoxOverride.Checked, radioButtonNormal.Checked, radioButtonFolder.Checked, radioButtonZIP.Checked, checkBoxDate.Checked };
            string fname        = textBoxName.Text;

            if (comboBoxFilestobackup.Items.Count == 0) //if no files to backup are given
            {
                displayText("No file selected!", 1500, "red");
                return;
            }

            if (comboBoxBackupLocations.Items.Count == 0) //if no backup locations are given
            {
                displayText("No backup path set!", 1500, "red");
                return;
            }

            //fill the lists with the items in the comboboxes
            foreach (string s in comboBoxBackupLocations.Items)
            {
                backupLocations.Add(s);
            }
            foreach (string s in comboBoxFilestobackup.Items)
            {
                backupFiles.Add(s);
            }

            #region check if files, directories exist

            //check if directories still exist
            foreach (string s in backupLocations)
            {
                if (!Directory.Exists(s))
                {
                    displayText("'" + s + "' is not a valid location!", 1500, "red");
                    return;
                }
            }

            //check if files still exist
            foreach (string s in backupFiles)
            {
                if (!File.Exists(s))
                {
                    displayText("'" + s + "' does not exist! Did you delete or move the file?", 1500, "red");
                    return;
                }
            }

            //check if the folder/zip is named
            if ((fileSettings[(int)FSettings.SubFolder] || fileSettings[(int)FSettings.ZipFile]) && (textBoxName.Text.Equals("Name Folder/.zip") || (textBoxName.Text.Trim().Equals(""))))
            {
                displayText("Please enter a name for your Folder/.zip!", 1500, "red");
                return;
            }

            #endregion

            #region prepare and create folders

            if (fileSettings[(int)FSettings.SubFolder])
            {
                foreach (string l in backupLocations)
                {
                    try
                    {
                        //only create if not created yet, add date if the checkbox was checked
                        if (fileSettings[(int)FSettings.AddDateToFileName] && !Directory.Exists(Path.Combine(l, fname + "_" + dt.Day + "-" + dt.Month + "-" + dt.Year)))
                        {
                            Directory.CreateDirectory(Path.Combine(l, fname + "_" + dt.Day + "-" + dt.Month + "-" + dt.Year));
                        }
                        else if (!Directory.Exists(Path.Combine(l, fname)))
                        {
                            Directory.CreateDirectory(Path.Combine(l, fname));
                        }
                    }
                    catch (Exception ex)
                    {
                        displayText("Not able to create folder at '" + l + "' maybe invalid foldername or missing permissions?", 3000, "red");
                        Console.WriteLine(ex);
                        return;
                    }
                }
            }

            #endregion

            //used to give the background worker the information he needs
            var bDataEventArgs = new BackupDataArgs(dt, backupLocations, backupFiles, fileSettings, fname);

            progressBarMain.Value    = 0;
            buttonOK.Enabled         = false;                      //disable controls in the meantime
            groupBoxSettings.Enabled = false;
            backgroundWorkerBackup.RunWorkerAsync(bDataEventArgs); //everything is alright, we are ready to backup
        }