コード例 #1
0
ファイル: Core.cs プロジェクト: trailmax/cacheCopy
        /// <summary>
        /// Executes the main routine: reads information from form
        /// and accordingly to the settings copies the files from one place to another
        /// </summary>
        public void executeMainRoutine(BackgroundWorker worker, DoWorkEventArgs e)
        {
            try
            {
                mainGUI.setProgressLabel("Reading files");

                List <String> errors = new List <string>();

                string source = mainGUI.GetSourceFolder();
                readFiles(source);

                // read data about filters from GUI and set the filters in Core
                setFilters();

                mainGUI.setProgressLabel("Filtering files");
                // apply filters to files and save error messages from there
                applyFilters(worker, e);

                int totalFilesCopied = 0;
                // finally copy the files
                string targetFolder = mainGUI.GetTargetFolder();
                for (int i = 0; i < files.Count; i++)
                {
                    FileInfo file = files[i];
                    mainGUI.setProgressLabel("Copying files: " + i.ToString() + "/" + files.Count.ToString());
                    if (worker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        errors.Add("Cancelled by user");
                        break;
                    }

                    // now make a new filename, according to the parameters provided by user
                    string paddedNumber = Util.PadNumberToMaximum(i, files.Count);
                    string newPath      = FileNaming.GenerateFileName(file, mainGUI.GetFileNamingPattern(), mainGUI.IsAllowOverwriteFiles(), targetFolder, paddedNumber);

                    try
                    {
                        file.CopyTo(newPath);
                        totalFilesCopied++;

                        // if user checked the box for file deletion, try to remove the file.
                        if (mainGUI.IsRemoveImagesFromCache())
                        {
                            file.Delete();
                        }
                    }
                    catch (IOException)
                    {
                        errors.Add("Could not copy or delete file: " + file.Name);
                    }

                    // update progress bar
                    reportProgress(files.Count, i, worker);
                }
                mainGUI.setProgressLabel("");
                //return errors;
                e.Result = totalFilesCopied;
            }
            catch (Exception ex)
            {
                Util.WriteToLogFile(ex);
                throw;
            }
        }