Example #1
0
        void FileCopier(ManualResetEvent ProcessVerif, Work work, bool priorities)
        {
            differential.remainingfiles = differential.filecount;
            differential.remainingsize  = differential.totalsize;
            DirectoryInfo path = new DirectoryInfo(differential.source);

            FileInfo[] files = path.GetFiles();
            //Observer observable implementation
            ProgressBarReporter reporter = new ProgressBarReporter();
            ProgressBarTracker  provider = new ProgressBarTracker();

            reporter.Subscribe(provider);
            Console.WriteLine("Starting process...");

            //Initializating the timer and starting it
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            // Create subdirectory structure in destination
            foreach (string direction in System.IO.Directory.GetDirectories(differential.source, "*", System.IO.SearchOption.AllDirectories))
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.Combine(differential.destination, direction.Substring(differential.source.Length - 5)));
            }

            // Copy each file in its final direction
            foreach (string file in System.IO.Directory.GetFiles(differential.source, "*", System.IO.SearchOption.AllDirectories))
            {
                ProcessVerif.WaitOne();
                Console.WriteLine("copying " + file);
                differential.currentfiletocopy = file;

                //Create a FileInfo object "f" with its directory to get the f.Length object's size (in bytes)
                FileInfo fs    = new FileInfo(file);
                string   dfile = differential.destination + "/" + file.Remove(0, differential.source.Length);
                FileInfo fd    = new FileInfo(dfile);
                //We first save file with a priority
                if (priorities == true)
                {
                    foreach (string prio in work.priorities)
                    {
                        if (prio == (Path.Combine(differential.destination, file.Substring(differential.destination.Length - 5)).Split('.')[1]))
                        {
                            if (fs.LastWriteTime > fd.LastWriteTime)
                            {
                                //Copy File
                                Console.WriteLine("Source file is more recent ");
                                System.IO.File.Copy(file, System.IO.Path.Combine(differential.destination, file.Substring(differential.destination.Length - 5)), true);
                                Console.WriteLine("Copy has been to be executed");
                                CryptFile(Path.Combine(differential.destination, file.Substring(differential.destination.Length - 5)));
                            }
                            else
                            {
                                //Tellthe user no copy is needed
                                Console.WriteLine(" Destination file is the same than source file, no copy executed");
                            }
                            differential.remainingfiles--;
                            differential.remainingsize -= fs.Length;
                            differential.time           = stopwatch.Elapsed.Seconds;
                            differential.RefreshSaveStat(differential);
                            RunningTasks.AllTasks.Remove(work.informations);
                            work.informations.Progress = 100 - ((differential.remainingsize * 100) / differential.totalsize);
                            provider.TrackProgress(new TaskProgressBar(work.informations.Progress.ToString()));
                        }
                    }
                    provider.EndTransmission();
                }
                else //then we relaunch the method to save all other files
                {
                    foreach (string prio in work.priorities)
                    {
                        if (prio == (Path.Combine(differential.destination, file.Substring(differential.destination.Length - 5)).Split('.')[1]))
                        {
                            if (fs.LastWriteTime > fd.LastWriteTime)
                            {
                                //Copy File
                                Console.WriteLine("Source file is more recent ");
                                System.IO.File.Copy(file, System.IO.Path.Combine(differential.destination, file.Substring(differential.destination.Length - 5)), true);
                                Console.WriteLine("Copy has been to be executed");
                                CryptFile(Path.Combine(differential.destination, file.Substring(differential.destination.Length - 5)));
                            }
                            else
                            {
                                //Tellthe user no copy is needed
                                Console.WriteLine(" Destination file is the same than source file, no copy executed");
                            }
                            differential.remainingfiles--;
                            differential.remainingsize -= fs.Length;
                            differential.time           = stopwatch.Elapsed.Seconds;
                            differential.RefreshSaveStat(differential);
                            RunningTasks.AllTasks.Remove(work.informations);
                            //notifying observers
                            work.informations.Progress = 100 - ((differential.remainingsize * 100) / differential.totalsize);
                            provider.TrackProgress(new TaskProgressBar(work.informations.Progress.ToString()));
                        }
                    }
                    provider.EndTransmission();
                }
            }
        }
Example #2
0
        //Copying files one by one from source to destination

        void FileCopier(ManualResetEvent ProcessVerif, Work work, bool priorities)
        {
            mirror.remainingfiles = mirror.filecount;
            mirror.remainingsize  = mirror.totalsize;
            DirectoryInfo path = new DirectoryInfo(mirror.source);

            FileInfo[] files = path.GetFiles();
            Console.WriteLine("Starting process...");

            //Initializating the timer and starting it
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            //Implementing observer and observable
            ProgressBarReporter reporter = new ProgressBarReporter();
            ProgressBarTracker  provider = new ProgressBarTracker();

            reporter.Subscribe(provider);

            // Create subdirectory structure in destination
            foreach (string direction in System.IO.Directory.GetDirectories(mirror.source, "*", System.IO.SearchOption.AllDirectories))
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.Combine(mirror.destination, direction.Substring(mirror.source.Length - 5)));
            }
            //We first save file with a priority
            if (priorities)
            {
                foreach (string file in System.IO.Directory.GetFiles(mirror.source, "*", System.IO.SearchOption.AllDirectories))
                {
                    if (work.informations.IsRunning == false)
                    {
                        work.informations.RunningState.WaitOne();
                    }

                    foreach (string prio in work.priorities)
                    {
                        if (prio == (Path.Combine(mirror.destination, file.Substring(mirror.destination.Length - 5)).Split('.')[1]))
                        {
                            ProcessVerif.WaitOne();
                            Console.WriteLine("copying " + file);
                            mirror.currentfiletocopy = file;
                            System.IO.File.Copy(file, System.IO.Path.Combine(mirror.destination, file.Substring(mirror.destination.Length - 5)), true);
                            mirror.remainingfiles--;
                            //Create a FileInfo object "f" with its directory to get the f.Length object's size (in bytes)
                            FileInfo f = new FileInfo(file);
                            work.informations.Progress = 100 - ((mirror.remainingsize * 100) / mirror.totalsize);
                            //notifying progress to the observer
                            provider.TrackProgress(new TaskProgressBar(work.informations.Progress.ToString()));
                            mirror.remainingsize -= f.Length;
                            mirror.time           = stopwatch.Elapsed.Seconds;
                            CryptFile(Path.Combine(mirror.destination, file.Substring(mirror.destination.Length - 5)));
                            mirror.RefreshSaveStat(mirror);
                        }
                    }
                }
                provider.TrackProgress(new TaskProgressBar("100"));
                provider.EndTransmission();
            }
            //Then we save all other files
            else
            {
                foreach (string file in System.IO.Directory.GetFiles(mirror.source, "*", System.IO.SearchOption.AllDirectories))
                {
                    if (work.informations.IsRunning == false)
                    {
                        work.informations.RunningState.WaitOne();
                    }

                    foreach (string prio in work.priorities)
                    {
                        if (prio != (Path.Combine(mirror.destination, file.Substring(mirror.destination.Length - 5)).Split('.')[1]))
                        {
                            ProcessVerif.WaitOne();
                            Console.WriteLine("copying " + file);
                            mirror.currentfiletocopy = file;
                            System.IO.File.Copy(file, System.IO.Path.Combine(mirror.destination, file.Substring(mirror.destination.Length - 5)), true);
                            mirror.remainingfiles--;
                            //Create a FileInfo object "f" with its directory to get the f.Length object's size (in bytes)
                            FileInfo f = new FileInfo(file);
                            work.informations.Progress = 100 - ((mirror.remainingsize * 100) / mirror.totalsize);
                            provider.TrackProgress(new TaskProgressBar(work.informations.Progress.ToString()));
                            mirror.remainingsize -= f.Length;
                            mirror.time           = stopwatch.Elapsed.Seconds;
                            CryptFile(Path.Combine(mirror.destination, file.Substring(mirror.destination.Length - 5)));
                            mirror.RefreshSaveStat(mirror);
                        }
                    }
                }
                provider.TrackProgress(new TaskProgressBar("100"));
                provider.EndTransmission();
            }
        }