Inheritance: System.Windows.Forms.Form
Exemplo n.º 1
0
        private void buttonStart_Click( object sender, EventArgs e )
        {
            if ( !bStarted )
            {
                // Disable all controls except start button that now becomes cancel
                panelInfos.Enabled = false;
                buttonStart.Text = "Cancel";
                bStarted = true;
                bCancel = false;

                // Estimate total size
                Dictionary<FileInfo,string>	ErrorFiles = new Dictionary<FileInfo,string>();
                long	TotalSize = 0;
                long	TotalFiles = 0;
                progressBar1.Style = ProgressBarStyle.Marquee;
                for ( int i=0; i < SOURCE_FOLDERS_COUNT && !bCancel; i++ )
                    RecurseVisit( m_SourceFolders[i], ( FileInfo _File ) =>
                        {
                            try
                            {
                                TotalSize += _File.Length;
                                TotalFiles++;

                                // Check events every 256 files for a chance to press cancel...
                                if ( (TotalFiles & 0xFF) == 0 )
                                    Application.DoEvents();
                            }
                            catch ( Exception _e )
                            {
                                ErrorFiles.Add( _File, _e.Message );
                            }

                            return !bCancel;
                        } );

                // Start copying
                long	UpdateProgressFilesCount = Math.Max( 1, TotalFiles / 100 );
                long	SumCopiedSize = 0;
                long	TotalCopiedFiles = 0;
                progressBar1.Style = ProgressBarStyle.Continuous;

                string	TargetFolderName = m_TargetFolder.FullName;
                if ( checkBoxCreateDateFolder.Checked )
                {	// Append current date to destination
                    DateTime	Now = DateTime.Now;
                    string	TimeStamp = Now.Year.ToString( "D04" ) + "-" + Now.Month.ToString( "D02" ) + "-" + Now.Day.ToString( "D02" ) + "_" + Now.Hour.ToString( "D02" ) + "." + Now.Minute.ToString( "D02" );
                    TargetFolderName += "\\" + TimeStamp;
                }

                for ( int i=0; i < SOURCE_FOLDERS_COUNT && !bCancel; i++ )
                {
                    try
                    {
                        string	SourceFolder = m_SourceFolders[i].FullName;
                        int		SourceFolderNameLength = SourceFolder.LastIndexOf( '\\' );
                        if ( SourceFolderNameLength == -1 )
                        {	// Means we're copying an entire drive?
                            SourceFolderNameLength = SourceFolder.LastIndexOf( ':' );
                            if ( SourceFolderNameLength == -1 )
                                throw new Exception( "Can't isolate folder name for source folder \"" + SourceFolder + "\"... Can't backup that folder!" );
                            SourceFolderNameLength++;
                        }

                        RecurseVisit( m_SourceFolders[i], ( FileInfo _File ) =>
                            {
                                // Skip files that posed problem in the first place...
                                if ( ErrorFiles.ContainsKey( _File ) )
                                    return !bCancel;

                                try
                                {
                                    // Update progress
                                    SumCopiedSize += _File.Length;
                                    TotalCopiedFiles++;
                                    if ( (TotalCopiedFiles % UpdateProgressFilesCount) == 0 )
                                    {
                                        progressBar1.Value = (int) ((progressBar1.Maximum * SumCopiedSize) / TotalSize);
                                        Application.DoEvents();
                                    }

                                    // Attempt copy
                                    string	DirectoryWithoutRoot = _File.FullName.Remove( 0, SourceFolderNameLength );
                                    string	TargetFileName = TargetFolderName + DirectoryWithoutRoot;

                                    Directory.CreateDirectory( Path.GetDirectoryName( TargetFileName ) );

                                    _File.CopyTo( TargetFileName, true );
                                }
                                catch ( Exception _e )
                                {
                                    ErrorFiles.Add( _File, _e.Message );
                                }

                                return !bCancel;
                            } );

                        if ( bCancel )
                            throw new Exception( "Cancelled by user..." );
                    }
                    catch ( Exception _e )
                    {
                        ErrorFiles.Add( new FileInfo( m_SourceFolders[i].FullName ), _e.Message );	// can't backup!
                    }
                }

                if ( ErrorFiles.Count > 0 )
                {
                    if ( MessageBox.Show( this, "Copy is finished with errors for " + ErrorFiles.Count + " files out of " + TotalFiles + " (" + (100*TotalCopiedFiles / TotalFiles) + "% files copied)\r\nDo you want to see the log?", "Backup", MessageBoxButtons.YesNo, MessageBoxIcon.Error ) == DialogResult.Yes )
                    {	// Show log...
                        LogForm	Log = new LogForm();
                                Log.Errors = ErrorFiles;
                        Log.ShowDialog( this );
                    }
                }
                else
                    MessageBox.Show( this, "Success!  " + TotalFiles + " files copied...", "Backup", MessageBoxButtons.OK, MessageBoxIcon.Information );

                bStarted = false;
            }
            else
            {	// Cancel
                bCancel = true;
                return;
            }

            // Enable all controls and bring back everything to normal
            buttonStart.Text = "Start Backup";
            panelInfos.Enabled = true;
            progressBar1.Value = 0;
        }
Exemplo n.º 2
0
        private void buttonStart_Click(object sender, EventArgs e)
        {
            if (!bStarted)
            {
                // Disable all controls except start button that now becomes cancel
                panelInfos.Enabled = false;
                buttonStart.Text   = "Cancel";
                bStarted           = true;
                bCancel            = false;

                // Estimate total size
                Dictionary <FileInfo, string> ErrorFiles = new Dictionary <FileInfo, string>();
                long TotalSize  = 0;
                long TotalFiles = 0;
                progressBar1.Style = ProgressBarStyle.Marquee;
                for (int i = 0; i < SOURCE_FOLDERS_COUNT && !bCancel; i++)
                {
                    RecurseVisit(m_SourceFolders[i], ( FileInfo _File ) =>
                    {
                        try
                        {
                            TotalSize += _File.Length;
                            TotalFiles++;

                            // Check events every 256 files for a chance to press cancel...
                            if ((TotalFiles & 0xFF) == 0)
                            {
                                Application.DoEvents();
                            }
                        }
                        catch (Exception _e)
                        {
                            ErrorFiles.Add(_File, _e.Message);
                        }

                        return(!bCancel);
                    });
                }

                // Start copying
                long UpdateProgressFilesCount = Math.Max(1, TotalFiles / 100);
                long SumCopiedSize            = 0;
                long TotalCopiedFiles         = 0;
                progressBar1.Style = ProgressBarStyle.Continuous;

                string TargetFolderName = m_TargetFolder.FullName;
                if (checkBoxCreateDateFolder.Checked)
                {                       // Append current date to destination
                    DateTime Now       = DateTime.Now;
                    string   TimeStamp = Now.Year.ToString("D04") + "-" + Now.Month.ToString("D02") + "-" + Now.Day.ToString("D02") + "_" + Now.Hour.ToString("D02") + "." + Now.Minute.ToString("D02");
                    TargetFolderName += "\\" + TimeStamp;
                }

                for (int i = 0; i < SOURCE_FOLDERS_COUNT && !bCancel; i++)
                {
                    try
                    {
                        string SourceFolder           = m_SourceFolders[i].FullName;
                        int    SourceFolderNameLength = SourceFolder.LastIndexOf('\\');
                        if (SourceFolderNameLength == -1)
                        {                               // Means we're copying an entire drive?
                            SourceFolderNameLength = SourceFolder.LastIndexOf(':');
                            if (SourceFolderNameLength == -1)
                            {
                                throw new Exception("Can't isolate folder name for source folder \"" + SourceFolder + "\"... Can't backup that folder!");
                            }
                            SourceFolderNameLength++;
                        }

                        RecurseVisit(m_SourceFolders[i], ( FileInfo _File ) =>
                        {
                            // Skip files that posed problem in the first place...
                            if (ErrorFiles.ContainsKey(_File))
                            {
                                return(!bCancel);
                            }

                            try
                            {
                                // Update progress
                                SumCopiedSize += _File.Length;
                                TotalCopiedFiles++;
                                if ((TotalCopiedFiles % UpdateProgressFilesCount) == 0)
                                {
                                    progressBar1.Value = (int)((progressBar1.Maximum * SumCopiedSize) / TotalSize);
                                    Application.DoEvents();
                                }

                                // Attempt copy
                                string DirectoryWithoutRoot = _File.FullName.Remove(0, SourceFolderNameLength);
                                string TargetFileName       = TargetFolderName + DirectoryWithoutRoot;

                                Directory.CreateDirectory(Path.GetDirectoryName(TargetFileName));

                                _File.CopyTo(TargetFileName, true);
                            }
                            catch (Exception _e)
                            {
                                ErrorFiles.Add(_File, _e.Message);
                            }

                            return(!bCancel);
                        });

                        if (bCancel)
                        {
                            throw new Exception("Cancelled by user...");
                        }
                    }
                    catch (Exception _e)
                    {
                        ErrorFiles.Add(new FileInfo(m_SourceFolders[i].FullName), _e.Message);                                  // can't backup!
                    }
                }

                if (ErrorFiles.Count > 0)
                {
                    if (MessageBox.Show(this, "Copy is finished with errors for " + ErrorFiles.Count + " files out of " + TotalFiles + " (" + (100 * TotalCopiedFiles / TotalFiles) + "% files copied)\r\nDo you want to see the log?", "Backup", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes)
                    {                           // Show log...
                        LogForm Log = new LogForm();
                        Log.Errors = ErrorFiles;
                        Log.ShowDialog(this);
                    }
                }
                else
                {
                    MessageBox.Show(this, "Success!  " + TotalFiles + " files copied...", "Backup", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                bStarted = false;
            }
            else
            {                   // Cancel
                bCancel = true;
                return;
            }

            // Enable all controls and bring back everything to normal
            buttonStart.Text   = "Start Backup";
            panelInfos.Enabled = true;
            progressBar1.Value = 0;
        }