Exemplo n.º 1
0
        //private void COPY_FILES_WITH_PROGRESSBAR()
        //{
        //    try
        //    {
        //        BackgroundWorker worker = new BackgroundWorker();
        //        worker.WorkerReportsProgress = true;
        //        worker.DoWork += worker_DoWork;
        //        worker.ProgressChanged += worker_ProgressChanged;
        //        worker.RunWorkerAsync();

        //    }
        //    catch (Exception)
        //    {
        //        throw;
        //    }
        //}



        //private void worker_DoWork(object sender, DoWorkEventArgs e)
        //{
        //    try
        //    {

        //        foreach (string s in System.IO.Directory.GetFiles(SOURCE_FOLDER))
        //        {
        //            string _Filename = System.IO.Path.GetFileName(s);
        //            string _DestFile = System.IO.Path.Combine(DESTINATION_FOLDER, _Filename);
        //            System.IO.FileInfo fi_source = new System.IO.FileInfo(s);
        //            System.IO.FileInfo fi_destination = null;

        //            if (System.IO.File.Exists(_DestFile) == true)
        //                fi_destination = new System.IO.FileInfo(_DestFile);

        //            if (fi_destination != null)
        //            {
        //                if (fi_source.LastWriteTime > fi_destination.LastWriteTime)
        //                {
        //                    Task.Run(() =>
        //                    {
        //                        fi_source.Copyfile(fi_destination, x => MyProgressBar.Dispatcher.BeginInvoke(new Action(() => { MyProgressBar.Value = x; })));
        //                    }).GetAwaiter().OnCompleted(() => MyProgressBar.Dispatcher.BeginInvoke(new Action(() => { MyProgressBar.Value = 100; })));
        //                }
        //            }
        //        }
        //    }
        //    catch (Exception)
        //    {

        //        throw;
        //    }
        //}

        private void COPY_FILES_WITH_PROGRESSBAR()
        {
            try
            {
                int Files_Count     = System.IO.Directory.GetFiles(SOURCE_FOLDER).Count();
                int Files_Completed = 0;

                foreach (string s in System.IO.Directory.GetFiles(SOURCE_FOLDER))
                {
                    string _Filename = System.IO.Path.GetFileName(s);
                    string _DestFile = System.IO.Path.Combine(DESTINATION_FOLDER, _Filename);

                    System.IO.FileInfo fi_source      = new System.IO.FileInfo(s);
                    System.IO.FileInfo fi_destination = new System.IO.FileInfo(_DestFile);

                    if (System.IO.File.Exists(_DestFile) == true)
                    {
                        this.Focus();
                        if (fi_source.LastWriteTime > fi_destination.LastWriteTime)
                        {
                            System.IO.File.Delete(_DestFile);

                            Task.Run(() =>
                            {
                                fi_source.Copyfile(fi_destination, x => MyProgressBar.Dispatcher.BeginInvoke(new Action(() =>
                                {
                                    MyProgressBar.Value      = x;
                                    BrdInstall.Visibility    = Visibility.Hidden;
                                    TxtbFileBeingCopied.Text = System.IO.Path.GetFileName(_DestFile) + " - " + MyProgressBar.Value.ToString() + "%";
                                })));
                            }).GetAwaiter().OnCompleted(() => MyProgressBar.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                MyProgressBar.Value      = 100;
                                TxtbFileBeingCopied.Text = "COMPLETED";
                                Files_Completed++;
                                if (Files_Completed == Files_Count)
                                {
                                    BrdInstall.Visibility = Visibility.Visible;
                                    BtnInstall_Click(null, null);
                                }
                            })));
                        }
                        else
                        {
                            Files_Completed++;
                            BrdInstall.Visibility = Visibility.Visible;
                            BtnInstall_Click(null, null);
                        }
                    }
                    else
                    {
                        this.Focus();
                        Task.Run(() =>
                        {
                            fi_source.Copyfile(fi_destination, x => MyProgressBar.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                MyProgressBar.Value      = x;
                                BrdInstall.Visibility    = Visibility.Hidden;
                                TxtbFileBeingCopied.Text = System.IO.Path.GetFileName(_DestFile) + " - " + MyProgressBar.Value.ToString() + "%";
                            })));
                        }).GetAwaiter().OnCompleted(() => MyProgressBar.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            MyProgressBar.Value      = 100;
                            TxtbFileBeingCopied.Text = "COMPLETED";
                            Files_Completed++;
                            if (Files_Completed == Files_Count)
                            {
                                BrdInstall.Visibility = Visibility.Visible;
                                BtnInstall_Click(null, null);
                            }
                        })));
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        private void START_THE_COPY()
        {
            try
            {
                /// CHECK PATH OF INSTALLER
                Path_Installer = VALIDATE_INSTALLER_PATH(Path_Installer);

                /// OPEN CONNECTION TO NETWORK SHARE
                using (System.Diagnostics.Process Proc = new System.Diagnostics.Process())
                {
                    Proc.StartInfo.FileName    = @"C:\Windows\System32\net.exe";
                    Proc.StartInfo.Arguments   = "use \"" + Path_Installer + "\" /user:\"" + NetShare_Username + "\" " + NetShare_Password;
                    Proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                    Proc.Start();
                    Proc.WaitForExit();
                }

                /// START COPYING
                int index = 0;
                foreach (string s in System.IO.Directory.GetFiles(Path_Installer))
                {
                    index++;
                    var file_destination              = System.IO.Path.Combine(LOCAL_DEST, System.IO.Path.GetFileName(s));
                    System.IO.FileInfo fi_source      = new System.IO.FileInfo(s);
                    System.IO.FileInfo fi_destination = new System.IO.FileInfo(file_destination);

                    /// CHECK IF DESTINATION FILE ALREADY EXISTS
                    if (System.IO.File.Exists(file_destination))
                    {
                        if (fi_source.LastWriteTime > fi_destination.LastWriteTime)
                        {
                            System.IO.File.Delete(file_destination);
                        }
                    }

                    Task.Run(() =>
                    {
                        fi_source.Copyfile(fi_destination, x => MyPGB.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            MyPGB.Value = x;
                            // BrdInstall.Visibility = Visibility.Hidden;
                            TxtbFileInProgress.Text = System.IO.Path.GetFileName(s) + " - " + MyPGB.Value.ToString() + "%";
                        })));
                    }).GetAwaiter().OnCompleted(() => MyPGB.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        MyPGB.Value             = 100;
                        TxtbFileInProgress.Text = "COMPLETED";

                        //START INSTALL
                        if (System.IO.Directory.GetFiles(Path_Installer).Count() == index)
                        {
                            BrdInstall.Visibility = Visibility.Visible;
                        }
                    })));
                }

                /// CLOSE CONNECTION TO NETWORK SHARE
                using (System.Diagnostics.Process Proc = new System.Diagnostics.Process())
                {
                    Proc.StartInfo.FileName    = @"C:\Windows\System32\net.exe";
                    Proc.StartInfo.Arguments   = "use " + Path_Installer + " /D";
                    Proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                    Proc.Start();
                    Proc.WaitForExit();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }