public static string Show(string Text, string Caption, string btnYes, string btnNo, string btnKeepFiles) { MsgBox = new MessageBoxForExisting(); MsgBox.label1.Text = Text; MsgBox.buttonYes.Text = btnYes; MsgBox.buttonNo.Text = btnNo; MsgBox.buttonKeepFiles.Text = btnKeepFiles; MsgBox.Text = Caption; result = btnKeepFiles; MsgBox.ShowDialog(); return(result); }
public static string Show(string Text, string Caption, string btnYes, string btnNo, string btnKeepFiles) { MsgBox = new MessageBoxForExisting(); MsgBox.label1.Text = Text; MsgBox.buttonYes.Text = btnYes; MsgBox.buttonNo.Text = btnNo; MsgBox.buttonKeepFiles.Text = btnKeepFiles; MsgBox.Text = Caption; result = btnKeepFiles; MsgBox.ShowDialog(); return result; }
//If Directory is transmitting from source to destination then this function will be called (not 'CopySingleFile' function) private void CopyDirectory(DirectoryInfo source, DirectoryInfo destination) { string cutDestination = destination.FullName.Split('.').Last(); if (cancelOperation != 1) { i = 2; try { //size = Directory.GetFiles(Source.Text, "*.*", SearchOption.AllDirectories).Length; //ProgressBar.Maximum = size; if (Total == 0) { totalCount = GetDirectorySize(Source.Text); } Total = 1; Random rnd = new Random(); #region Ask for deleting the file/directory of same as source //If folder already existed and it contains files and folders which is already present on destination //place. Then ask for deleting that file string cutDestinationName = ""; if (destination.Exists) { if (destination.Name == source.Name) { if (destination.Name.Length > 38) { cutDestinationName = destination.Name.Substring(0, 20) + "..." + destination.Name.Substring(destination.Name.Length - 15, 15); } else { cutDestinationName = destination.Name; } try { //string nameForDi = destination.FullName; DirectoryInfo destDriveName = destination.Root; string dExist1; //DialogResult dExist1; if (destDriveName.ToString() == Destination.Text) { dExist1 = MessageBoxForExisting.Show("Drive '" + cutDestinationName + "' is already Existed. Do You Want To Delete!!!", "Deleting Confirmation", "Yes", "No", "Keep Both"); } else { dExist1 = MessageBoxForExisting.Show("Folder '" + cutDestinationName + "' is already Existed. Do You Want To Delete!!! ", "Deleting Confirmation", "Yes", "No", "Keep Both"); } if (dExist1.ToString() == "Yes") //Deleting the whole directory if it is already existed { destination.Delete(true); valueDeleated = 1; } //Condition for keeping both folders else if (dExist1.ToString() == "Keep Both") { int noPlaced = 2; bool set = true; destination = new DirectoryInfo(destination.FullName); do { //Change the folder name with the new folder name for (int beforeNoPlaced = 1; beforeNoPlaced <= noPlaced; beforeNoPlaced++) { destination = new DirectoryInfo(destination.FullName.Replace(" (" + beforeNoPlaced + ")", "") + " (" + noPlaced + ")"); } DirectoryInfo[] checkTheFolderAlreadyExisted = destination.Parent.GetDirectories(destination.Name); //Checking whether folder name already exists. If yes then count be not equal to 0 if (checkTheFolderAlreadyExisted.Count() == 0) { set = false; } else { //Convert the folder name to the original name for (int beforeNoPlaced = 1; beforeNoPlaced <= noPlaced; beforeNoPlaced++) { destination = new DirectoryInfo(destination.FullName.Replace(" (" + beforeNoPlaced + ")", "")); } noPlaced++; } }while (set); //Loop will stop when condition is false } else { i = 1; } //if (destDriveName.ToString() == Destination.Text) //{ // dExist1 = MessageBox.Show("Drive '" + cutDestinationName + "' is already Existed. Do You Want To Delete!!! ", "Deleting Confirmation", MessageBoxButtons.YesNo); //} //else //{ // dExist1 = MessageBox.Show("Folder '" + cutDestinationName + "' is already Existed. Do You Want To Delete!!! ", "Deleting Confirmation", MessageBoxButtons.YesNo); //} //if (dExist1 == DialogResult.Yes) //Deleting the whole directory if it is already existed //{ // destination.Delete(true); // valueDeleated = 1; //} //else //{ // i = 1; //} } catch //if not able to delete (due to inaccessible file/folder) then this will be run { listBox.Items.Add(no + ") Directory '" + cutDestinationName + "' is not able to delete."); this.listBox.SelectedIndex = this.listBox.Items.Count - 1; this.Refresh(); no++; } } } //End of the logic1 #endregion if (!destination.Exists || valueDeleated == 1 && i != 1) //If destination name not existed or directory is already deleted then create the directory { destination.Create(); } #region Read each and every file/folder from the source and copy to destination area //Read each and every file from the source and copy to destination area by calling 'ControlUpdation' function otherwise exception thrown if reading of file is failed if (cancelOperation != 1) { if (i != 1) { FileInfo[] files = source.GetFiles(); foreach (FileInfo file in files) { if (file.Name.Length > 38) { getName = file.Name.Substring(0, 20) + "..." + file.Name.Substring(file.Name.Length - 15, 15); } else { getName = file.Name; } try { textSplit(); fileTransfer.Text = "Copying From '" + souName + "' To '" + destName + "' File Name: " + getName; this.Refresh(); string PathFile1 = Path.Combine(destination.FullName, file.Name); string PathFile2 = Path.Combine(source.FullName, file.Name); ControlUpdation(PathFile1, PathFile2); } catch (Exception) { listBox.Items.Add(no + ") File '" + getName + "' is not able to Copy"); try { totalCount -= file.Length; } catch { } this.listBox.SelectedIndex = this.listBox.Items.Count - 1; this.Refresh(); no++; } } //Read each and every directory from the source then call the same function again to check for files. //Directory's Name is also displayed from this code DirectoryInfo[] dirs = source.GetDirectories(); foreach (DirectoryInfo dir in dirs) { try { string destinationDir = Path.Combine(destination.FullName, dir.Name); DirectoryInfo destinfo = new DirectoryInfo(destinationDir); this.Refresh(); if (!destinfo.Exists) { CopyDirectory(dir, new DirectoryInfo(destinationDir)); } } catch (Exception) { string sourceFileNameRead = ""; if (dir.Name.Length > 38) { sourceFileNameRead = dir.Name.Substring(0, 20) + "..." + dir.Name.Substring(dir.Name.Length - 15, 15); } else { sourceFileNameRead = dir.Name; } listBox.Items.Add(no + ") Folder '" + sourceFileNameRead + "' is not able to Copy"); try { foreach (FileInfo f in dir.GetFiles("*.*")) { try { totalCount -= f.Length; } catch { continue; } } } catch { } this.listBox.SelectedIndex = this.listBox.Items.Count - 1; this.Refresh(); no++; } } } } } catch (Exception) { string sourceFolderNameRead = ""; if (source.Name.Length > 38) { sourceFolderNameRead = source.Name.Substring(0, 20) + "..." + source.Name.Substring(source.Name.Length - 15, 15); } else { sourceFolderNameRead = source.Name; } listBox.Items.Add(no + ") Unable to Copy the Folder '" + sourceFolderNameRead + "'"); try { foreach (FileInfo f in source.GetFiles("*.*")) { try { totalCount -= f.Length; } catch { continue; } } } catch { } this.listBox.SelectedIndex = this.listBox.Items.Count - 1; this.Refresh(); no++; } } //End of Logic2 #endregion }
//If single file is transmitting from source to destination then this function will be called (not 'CopyDirectory' function) private void CopySingleFile(FileInfo source, DirectoryInfo destination) { string cutDestination = destination.FullName.Split('.').Last(); //destination = new DirectoryInfo(destination.FullName.Replace("."+cutDestination,"")); if (cancelOperation != 1) { i = 2; totalCount = source.Length; FileInfo[] particularFileGetting = destination.Parent.GetFiles(destination.Name); if (particularFileGetting.Count() != 0) { if (destination.Name.Length > 38) //Splitting the name according to the length and display on the status bar after the 'File Name' { getName = source.Name.Substring(0, 20) + "..." + source.Name.Substring(destination.Name.Length - 15, 15); } else { getName = source.Name; } try { // FileInfo nameOfTheFileToDelete = new FileInfo(destination.Name); string fExit = MessageBoxForExisting.Show("File '" + getName + "' is already Existed. Do You Want To Delete!!! ", "Deleting Confirmation", "Yes", "No", "Keep Both"); if (fExit.ToString() == "Yes") { File.Delete(destination.FullName); } //Condition for keeping both files else if (fExit.ToString() == "Keep Both") { int noPlaced = 2; bool set = true; destination = new DirectoryInfo(destination.FullName.Replace("." + cutDestination, "")); do { //Folder name is already present and change its name to the new name for (int beforeNoPlaced = 1; beforeNoPlaced <= noPlaced; beforeNoPlaced++) { destination = new DirectoryInfo(destination.FullName.Replace(" (" + beforeNoPlaced + ")" + "." + cutDestination, "") + " (" + noPlaced + ")" + "." + cutDestination); } //Checking the file name is already present or not FileInfo[] checkTheFileAlreadyExisted = destination.Parent.GetFiles(destination.Name); //if file name not exists then count value be 0 if (checkTheFileAlreadyExisted.Count() == 0) { set = false; } //else replace the file name with the original name else { for (int beforeNoPlaced = 1; beforeNoPlaced <= noPlaced; beforeNoPlaced++) { destination = new DirectoryInfo(destination.FullName.Replace(" (" + beforeNoPlaced + ")" + "." + cutDestination, "")); } noPlaced++; } }while (set); } else { i = 1; } //DialogResult fExist = MessageBox.Show("File '" + getName + "' is already Existed. Do You Want To Delete!!! ", "Deleting Confirmation", MessageBoxButtons.YesNo); //if (fExist == DialogResult.Yes) //{ // File.Delete(destination.FullName); //} //else //{ // i = 1; //} } catch (Exception e) { MessageBox.Show("" + e); listBox.Items.Add(no + ") File '" + getName + "' is not able to Delete"); this.listBox.SelectedIndex = this.listBox.Items.Count - 1; this.Refresh(); no++; } } if (i != 1) { if (source.Name.Length > 38) //Splitting the name according to the length and display on the status bar after the 'File Name' { getName = source.Name.Substring(0, 20) + "..." + source.Name.Substring(source.Name.Length - 15, 15); } else { getName = source.Name; } try { textSplit(); fileTransfer.Text = "Copying From '" + souName + "' To '" + destName + "' File Name: " + getName; this.Refresh(); DirectoryInfo pathOfDirectory = destination.Parent; DateTime startTime = DateTime.Now; //source.CopyTo(destination.FullName); string PathFile1 = destination.FullName;//Path.Combine(destination.FullName, source.Name); string PathFile2 = source.FullName; ControlUpdation(PathFile1, PathFile2); //DateTime stopTime = DateTime.Now; //MessageBox.Show("" + stopTime); //MessageBox.Show("" + startTime); //TimeSpan difference = stopTime - startTime; //MessageBox.Show("" + difference); } catch (Exception) { listBox.Items.Add(no + ") File '" + getName + "' is not able to Copy"); this.listBox.SelectedIndex = this.listBox.Items.Count - 1; this.Refresh(); no++; } } } }