private bool BackupBeforeRestore()
        {
            string lErrorMessage = "";
            bool   lResult       = fBackupMaker.MakeBackup(fBackupTask, BackupType.btRestore, ref lErrorMessage);

            fBackupTask.SetLastBackupStatus(lResult, lErrorMessage);
            return(lResult);
        }
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            bool lShowHelp = false;

            fCommandLineOptions = new CommandLineParameters();
            if (CommandLine.Parser.Default.ParseArguments(e.Args, fCommandLineOptions))
            {
                if (!String.IsNullOrEmpty(fCommandLineOptions.DoBackup))
                {
                    if (fCommandLineOptions.DoBackup.StartsWith("="))
                    {
                        fCommandLineOptions.DoBackup = fCommandLineOptions.DoBackup.Remove(0, 1);
                    }

                    // user whant to make quick backup by Task Name
                    SettingsManager lSettings = new SettingsManager();
                    lSettings.LoadConfig();

                    AttachConsole(-1);

                    bool lFound = false;
                    foreach (BackupTask lBackupTask in lSettings.Settings.BackupTasks)
                    {
                        if (lBackupTask.Settings.Name == fCommandLineOptions.DoBackup)
                        {
                            lFound = true;

                            // do backup
                            BackupMaker lBackupMaker  = new BackupMaker();
                            string      lErrorMessage = "";

                            if (lBackupMaker.MakeBackup(lBackupTask, BackupType.btNormal, ref lErrorMessage))
                            {
                                Console.WriteLine("SUCCEED: Task '" + lBackupTask.Settings.Name + "' backup created");
                            }
                            else
                            {
                                Console.WriteLine("FAILED: Task '" + lBackupTask.Settings.Name + "' backup failed: \r\n" + lErrorMessage);
                            }
                            break;
                        }
                    }

                    if (!lFound)
                    {
                        Console.WriteLine("FAILED: Task '" + fCommandLineOptions.DoBackup + "' not found");
                    }

                    Environment.Exit(1);
                }
                else
                {
                    // ok, nothing more to do, parameters will be used in main app
                }

                // check if option -h, -help was used
                lShowHelp = fCommandLineOptions.ShowHelp;
            }
            else
            {
                //MessageBox.Show("Bad args!");
                lShowHelp = true;
            }

            // display help info in case of bad arguments or -h, -help options
            if (lShowHelp)
            {
                AttachConsole(-1);
                Console.WriteLine(fCommandLineOptions.GetUsage());
                Environment.Exit(1);
            }
        }
        private void CheckTasks()
        {
            bool  lRefreshComboBox       = false;
            Int64 lSettingsChangeCounter = 0;

            for (int lIndex = 0; lIndex < fSettings.Settings.BackupTasks.Count; ++lIndex)
            {
                BackupTask lBackupTask       = fSettings.Settings.BackupTasks[lIndex];
                bool       lItemUpdateNeeded = false;

                // calculate global ChangeCount of all task, it will be used to see if there was some change made recently
                // we calculate ChangeCount only for parameters that we want to check
                lSettingsChangeCounter += lBackupTask.ChangeCounter + lBackupTask.Settings.ChangeCounter;

                // if paths are wrong we can't do anything, go to next task
                if (lBackupTask.Settings.SourcePathHelper.IsEmpty || String.IsNullOrEmpty(lBackupTask.Settings.DestinationPath))
                {
                    continue;
                }

                // check flag from SourceFileWatcher
                if (lBackupTask.SourceFilesChanged)
                {
                    // if auto backup enabled and check frequency
                    if (lBackupTask.Settings.IsAutoBackupActive && fBackupMaker.CheckBackupFrequency(lBackupTask))
                    {
                        string lErrorMessage  = "";
                        bool   lErrorOccurred = false;

                        bool lFileWasModified = false;
                        bool lFileIsLocked    = false;

                        // we will do something, anything happens it GUI need to be updated
                        lItemUpdateNeeded = true;

                        // check for files modification, function result success/error
                        if (!fBackupMaker.CheckFilesToBackup(lBackupTask, ref lFileWasModified, ref lFileIsLocked, ref lErrorMessage))
                        {
                            // error occurred (or file is locked)
                            lRefreshComboBox = true;
                            lErrorOccurred   = true;
                            lBackupTask.SetLastBackupStatus(!lErrorOccurred, lErrorMessage);
                        }
                        else if (lFileWasModified)
                        {
                            // success and file was modified
                            lErrorOccurred = !fBackupMaker.MakeBackup(lBackupTask, BackupType.btNormal, ref lErrorMessage);
                            lBackupTask.SetLastBackupStatus(!lErrorOccurred, lErrorMessage);
                            lRefreshComboBox = true;
                        }
                        else
                        {
                            // no error (and files are not locked) but also nothing changed
                            if (!String.IsNullOrEmpty(lBackupTask.LastBackupErrorMessage))
                            {
                                lBackupTask.LastBackupErrorMessage = "";
                                lRefreshComboBox = true;
                            }
                        }

                        // backup was updated or there was no change that is important for us
                        // if error then try again latter (do not reset FilesChanged flag)
                        if (!lErrorOccurred)
                        {
                            lBackupTask.SourceFilesChanged = false;
                        }
                    }
                }

                // check flag from DestinationFileWatcher
                if (lBackupTask.DestinationDirectoryChanged)
                {
                    string lErrorMessage = "";
                    // to check if Size was changed
                    long lLastDestinationDirectorySize = lBackupTask.DestinationDirectorySize;

                    // backup was made so we need to update destination directory size once more
                    fBackupMaker.RefreshDestinationDirectorySize(lBackupTask);
                    // if destination directory size limit is set then remove older files
                    fBackupMaker.CleanUpOldBackups(lBackupTask, ref lErrorMessage);

                    lBackupTask.DestinationDirectoryChanged = false;

                    // if size is same as before skip updating
                    lItemUpdateNeeded = (lLastDestinationDirectorySize != lBackupTask.DestinationDirectorySize);
                }

                // if we made a backup of currently selected item we need to refresh data on GUI
                if (fSelectedBackupTaskIndex == lIndex)
                {
                    // check if something change or maybe we updated destination directory size
                    if (lItemUpdateNeeded)
                    {
                        SelectTaskByIndex(lIndex, false); // reselect same item, refresh interface
                    }
                }
            }

            fTasksCheckedAtLeastOnce = true;

            // Last Backup Time of some item changed, refresh list
            if (lRefreshComboBox)
            {
                FillComboBoxItems();
            }

            // save settings if something changed
            if (fSettingsChangeCounter != lSettingsChangeCounter)
            {
                if (fSettingsChangeCounter != -1)
                {
                    fSettings.SaveConfig();
                }
                fSettingsChangeCounter = lSettingsChangeCounter;
            }
        }