Example #1
0
        // This is called by diff.CompareEvent
        private void Compared(ComparisonResult result, FileSystemInfo[] files, bool isADir)
        {
            #region SizeDifferent
            if (result == ComparisonResult.SizeDifferent)              // Can only be files, not dirs
            {
                FileActions action = this.defSize;
                if (action == FileActions.Ask)
                {
                    action = this.AskWhatToDo(files, isADir, -1);                       // Need to ask user if
                }
                // default action is Ask

                if (action == FileActions.CancelCopy)                   // user choice: Cancel
                {
                    this.CancelNow();
                    return;
                }

                switch (action)
                {
                case FileActions.Ignore:                                // Do nothing
                    break;

                case FileActions.Delete:                                // The file(s) must be deleted
                    if (files[0].Exists)
                    {
                        files[0].Delete();
                    }
                    else
                    {
                        files[1].Delete();
                    }
                    break;

                // Copy by looking at the dates *************************************
                case FileActions.OverwriteNewer:                                                                        //	D
                    this.OverWriteDate((FileInfo)files[0], (FileInfo)files[1], false);                                  //
                    break;                                                                                              //	A

                //
                case FileActions.OverwriteOlder:                                                                        //	T
                    this.OverWriteDate((FileInfo)files[0], (FileInfo)files[1], true);                                   //
                    break;                                                                                              //	E

                //
                //********************************************************************
                case FileActions.Write1to2:                             // Writes the file to folder 2 (the file should exist)
                    try
                    {
                        ((FileInfo)files[0]).CopyTo(files[1].FullName, true);
                    }
                    catch (Exception e)                             // This should NOT happen (and it doesn't, usually...)
                    {
                        string[] f = new string[2];
                        f[0] = files[0].FullName;
                        f[1] = files[1].FullName;
                        this.ErrorEvent(e, f);
                    }
                    break;

                case FileActions.Write2to1:                             // Writes the file to folder 1 (the file should exist)
                    try
                    {
                        ((FileInfo)files[1]).CopyTo(files[0].FullName, true);
                    }
                    catch (Exception e)                              // This should NOT happen (and it doesn't, usually...)
                    {
                        string[] f = new string[2];
                        f[0] = files[0].FullName;
                        f[1] = files[1].FullName;
                        this.ErrorEvent(e, f);
                    }
                    break;
                }
            }
            #endregion

            #region MissingInFolderx
            if (result == ComparisonResult.MissingInFolder1 | result == ComparisonResult.MissingInFolder2)
            {
                FileActions action       = FileActions.Ask;
                int         missingIndex = 0;           // used only for the AskWhatToDo event
                // if default action is Ask

                if (result == ComparisonResult.MissingInFolder1)
                {
                    action       = this.defMissing1;
                    missingIndex = 1;
                }
                if (result == ComparisonResult.MissingInFolder2)
                {
                    action       = this.defMissing2;
                    missingIndex = 2;
                }

                if (action == FileActions.Ask)
                {
                    action = this.AskWhatToDo(files, isADir, missingIndex);
                }

                if (action == FileActions.CancelCopy)
                {
                    this.CancelNow();
                    return;
                }

                switch (action)
                {
                case FileActions.Ignore:
                    break;

                case FileActions.Copy:                           // Copy the missing file or dir

                    if (!isADir)                                 // It's not a dir (incredible!)
                    {
                        int missingFileIndex = missingIndex - 1; // missingFileIndex has to be 0-based
                        int presentFileIndex = Math.Abs(missingFileIndex - 1);

                        try
                        {
                            ((FileInfo)files[presentFileIndex]).CopyTo(files[missingFileIndex].FullName, false);
                        }
                        catch (IOException e)                                 // This should not happen because files[presentIndex] should not exist
                        {
                            string[] f = new string[2];
                            f[0] = files[0].FullName;
                            f[1] = files[1].FullName;
                            this.ErrorEvent(e, f);
                        }
                    }

                    else if (isADir)                                    // It's a dir (wow!)
                    {
                        int missingFolderIndex = missingIndex - 1;
                        int presentFolderIndex = Math.Abs(missingFolderIndex - 1);

                        RecursiveCopy rCopy = new RecursiveCopy(files[presentFolderIndex].FullName, files[missingFolderIndex].FullName, false);
                        rCopy.ErrorEvent += this.ErrorEvent;
                        rCopy.Copy();
                    }
                    break;

                case FileActions.Delete:                                        // The existing file or dir will be deleted
                    if (isADir)
                    {
                        if (files[0].Exists)
                        {
                            ((DirectoryInfo)files[0]).Delete(true);
                        }
                        else if (files[1].Exists)
                        {
                            ((DirectoryInfo)files[1]).Delete(true);
                        }
                    }
                    else
                    {
                        if (files[0].Exists)
                        {
                            ((FileInfo)files[0]).Delete();
                        }
                        else if (files[1].Exists)
                        {
                            ((FileInfo)files[1]).Delete();
                        }
                    }
                    break;

                case FileActions.Write1to2:                                     // Shall write the file or dir from folder index 0 to folder index 1
                    if (files[0].Exists & isADir)
                    {
                        RecursiveCopy rCopy = new RecursiveCopy(files[0].FullName, files[1].FullName, false);
                        rCopy.ErrorEvent += this.ErrorEvent;
                        rCopy.Copy();
                    }
                    else if (files[0].Exists & !isADir)
                    {
                        try
                        {
                            ((FileInfo)files[0]).CopyTo(files[1].FullName, false);                                      // Copies the file
                        }
                        catch (Exception e)
                        {
                            string[] f = new string[2];
                            f[0] = files[0].FullName;
                            f[1] = files[1].FullName;
                            this.ErrorEvent(e, f);
                        }
                    }
                    break;

                case FileActions.Write2to1:                         // Shall write the file or dir from folder index 1 to folder index 0
                    if (files[1].Exists & isADir)
                    {
                        RecursiveCopy rCopy = new RecursiveCopy(files[1].FullName, files[0].FullName, false);
                        rCopy.ErrorEvent += this.ErrorEvent;
                        rCopy.Copy();
                    }
                    else if (files[1].Exists & !isADir)
                    {
                        try
                        {
                            ((FileInfo)files[1]).CopyTo(files[0].FullName, false);
                        }
                        catch (Exception e)
                        {
                            string[] f = new string[2];
                            f[0] = files[0].FullName;
                            f[1] = files[1].FullName;
                            this.ErrorEvent(e, f);
                        }
                    }
                    break;
                }
            }
            #endregion
        }
Example #2
0
		// This is called by diff.CompareEvent
		private void Compared(ComparisonResult result, FileSystemInfo[] files, bool isADir)
		{
			#region SizeDifferent
			if (result == ComparisonResult.SizeDifferent)  // Can only be files, not dirs
			{
				FileActions action = this.defSize;
				if (action == FileActions.Ask)
					action = this.AskWhatToDo(files, isADir, -1);	// Need to ask user if 
																	// default action is Ask

				if (action == FileActions.CancelCopy)	// user choice: Cancel
				{
					this.CancelNow();
					return;
				}

				switch (action)
				{					
					case FileActions.Ignore:	// Do nothing
						break;

					case FileActions.Delete:	// The file(s) must be deleted
						if (files[0].Exists)
						{
							files[0].Delete();
						}
						else
						{
							files[1].Delete();
						}
						break;

						// Copy by looking at the dates *************************************
					case FileActions.OverwriteNewer:										//	D
						this.OverWriteDate((FileInfo)files[0], (FileInfo)files[1], false);	//	
						break;																//	A
																							//	
					case FileActions.OverwriteOlder:										//	T
						this.OverWriteDate((FileInfo)files[0], (FileInfo)files[1], true);	//	
						break;																//	E
																							//	
						//********************************************************************
					case FileActions.Write1to2:	// Writes the file to folder 2 (the file should exist)
						try
						{
							((FileInfo)files[0]).CopyTo(files[1].FullName, true);
						}
						catch (Exception e) // This should NOT happen (and it doesn't, usually...) 
						{
							string[] f = new string[2];
							f[0] = files[0].FullName;
							f[1] = files[1].FullName;
							this.ErrorEvent(e, f);
						}
						break;

					case FileActions.Write2to1:	// Writes the file to folder 1 (the file should exist)
						try
						{
							((FileInfo)files[1]).CopyTo(files[0].FullName, true);
						}
						catch (Exception e)  // This should NOT happen (and it doesn't, usually...)
						{
							string[] f = new string[2];
							f[0] = files[0].FullName;
							f[1] = files[1].FullName;
							this.ErrorEvent(e, f);
						}
						break;						
				}
			}
			#endregion

			#region MissingInFolderx
			if (result ==  ComparisonResult.MissingInFolder1 | result ==  ComparisonResult.MissingInFolder2)
			{
				FileActions action = FileActions.Ask;
				int missingIndex = 0;	// used only for the AskWhatToDo event 
										// if default action is Ask

				if (result == ComparisonResult.MissingInFolder1)
				{
					action = this.defMissing1;
					missingIndex = 1;
				}
				if (result == ComparisonResult.MissingInFolder2)
				{
					action = this.defMissing2;
					missingIndex = 2;
				}

				if (action == FileActions.Ask)
					action = this.AskWhatToDo(files, isADir, missingIndex);

				if (action == FileActions.CancelCopy)
				{
					this.CancelNow();
					return;
				}

				switch (action)
				{
					case FileActions.Ignore:
						break;

					case FileActions.Copy:					// Copy the missing file or dir
						
						if (!isADir) // It's not a dir (incredible!)
						{
							int missingFileIndex = missingIndex - 1; // missingFileIndex has to be 0-based
							int presentFileIndex = Math.Abs(missingFileIndex - 1);

							try
							{
								((FileInfo)files[presentFileIndex]).CopyTo(files[missingFileIndex].FullName, false);
							}
							catch (IOException e) // This should not happen because files[presentIndex] should not exist
							{
								string[] f = new string[2];
								f[0] = files[0].FullName;
								f[1] = files[1].FullName;
								this.ErrorEvent(e, f);
							}

						}
					
						else if (isADir)	// It's a dir (wow!)
						{
							int missingFolderIndex = missingIndex - 1;
							int presentFolderIndex = Math.Abs(missingFolderIndex - 1);

							RecursiveCopy rCopy = new RecursiveCopy(files[presentFolderIndex].FullName, files[missingFolderIndex].FullName, false);
							rCopy.ErrorEvent += this.ErrorEvent;
							rCopy.Copy();
						}
						break;

					case FileActions.Delete:		// The existing file or dir will be deleted
						if (isADir)
						{
							if (files[0].Exists)
								((DirectoryInfo)files[0]).Delete(true);
							else if (files[1].Exists)
								((DirectoryInfo)files[1]).Delete(true);
						}
						else
						{
							if (files[0].Exists)
								((FileInfo)files[0]).Delete();
							else if (files[1].Exists)
								((FileInfo)files[1]).Delete();
						}
						break;

					case FileActions.Write1to2:		// Shall write the file or dir from folder index 0 to folder index 1
						if (files[0].Exists & isADir)
						{
							RecursiveCopy rCopy = new RecursiveCopy(files[0].FullName, files[1].FullName, false);
							rCopy.ErrorEvent += this.ErrorEvent;
							rCopy.Copy();
						}
						else if (files[0].Exists & !isADir)
						{
							try
							{
								((FileInfo)files[0]).CopyTo(files[1].FullName, false);	// Copies the file
							}
							catch (Exception e)
							{
								string[] f = new string[2];
								f[0] = files[0].FullName;
								f[1] = files[1].FullName;
								this.ErrorEvent(e, f);
							}
						}
						break;

					case FileActions.Write2to1: // Shall write the file or dir from folder index 1 to folder index 0
						if (files[1].Exists & isADir)
						{
							RecursiveCopy rCopy = new RecursiveCopy(files[1].FullName, files[0].FullName, false);
							rCopy.ErrorEvent += this.ErrorEvent;
							rCopy.Copy();
						}
						else if (files[1].Exists & !isADir)
						{
							try
							{
								((FileInfo)files[1]).CopyTo(files[0].FullName, false);
							}
							catch (Exception e)
							{
								string[] f = new string[2];
								f[0] = files[0].FullName;
								f[1] = files[1].FullName;
								this.ErrorEvent(e, f);
							}
						}
						break;
				}
			}
			#endregion
		}