public void BackCopy(DirectoryInfo source, DirectoryInfo target) { if (Directory.Exists(target.FullName) == false) { Directory.CreateDirectory(target.FullName); ascProcess ap = new ascProcess(); Console.WriteLine("please wait. creating directory {0}", target.FullName); ap.threadWait(); } foreach (FileInfo fi in source.GetFiles()) { fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true); } foreach (DirectoryInfo diSourceDir in source.GetDirectories()) { DirectoryInfo nextTargetDir = target.CreateSubdirectory(diSourceDir.Name); BackCopy(diSourceDir, nextTargetDir); } }
public void Init() { ascProcess ascInit = new ascProcess(); string dbMessage, backup_status, copy_status, source, error; bool checkFile = false, checkPath = false; tmp = Console.Out; Console.WriteLine("\nAsctool version 1.0 "); Console.WriteLine("Apsmith (c) 2014 \n"); ascInit.threadWait(); try { if (ascInit.CheckConnection()) { try { dbMessage = "Database connection has been established."; Console.WriteLine(dbMessage); } catch (Exception ex) { Console.SetOut(sw); Console.WriteLine(ex.Message); sw.Close(); } ascInit.threadWait(); ascInit.dbExecute(); dbMessage = "SQL query execution complete."; Console.WriteLine("\n"); Console.WriteLine(dbMessage); ascInit.threadWait(); #region try IIS STOP try { ascInit.ExecuteCommandSync("iisreset /stop"); } catch (Exception ex) { Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n"); error = Console.ReadLine(); #region errorPath while (!checkPath) { if ((File.Exists(error) || Directory.Exists(error)) && checkPath == false) { fs = new FileStream(@"" + error + "(" + DateTime.Now.ToString("MM-dd-yy_HHmm") + ")" + ".txt", FileMode.Create); sw = new StreamWriter(fs); tmp = Console.Out; Console.SetOut(sw); Console.WriteLine(ex.Message); sw.Close(); Console.SetOut(tmp); Console.WriteLine(ex.Message); break; } Console.WriteLine("{0} Sorry your input is invalid. \n", error); Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n"); error = Console.ReadLine(); continue; } #endregion } #endregion #region while source backUp Console.WriteLine("\nAsctool version 1.0 "); Console.WriteLine("Apsmith (c) 2014 \n"); Console.WriteLine("Please enter source path: " + "(e.g. " + @"C:\foldername\.." + ") \n"); source = Console.ReadLine(); while (!checkFile) { if ((Directory.Exists(source)) && checkFile == false && ascInit.IsDirectoryEmpty(source) == false) { string sourceDirName = @"" + source; Console.WriteLine("\nPlease enter destination path: " + "(e.g. " + @"C:\foldername\.." + ") \n"); string dirSource = Console.ReadLine(); Console.WriteLine("\n"); string destDirName = @"" + dirSource + "(" + File.GetLastWriteTime(sourceDirName).ToString("MM-dd-yy") + ")"; if (Directory.Exists(sourceDirName)) { DirectoryInfo sourceInfo = new DirectoryInfo(sourceDirName); DirectoryInfo destInfo = new DirectoryInfo(destDirName); ascInit.BackCopy(sourceInfo, destInfo); backup_status = "Backup successful."; Console.WriteLine(backup_status); } else { backup_status = "File doesn't exists."; Console.WriteLine(backup_status); } break; } Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n"); error = Console.ReadLine(); #region errorPath while (!checkPath) { if ((File.Exists(error) || Directory.Exists(error)) && checkPath == false) { fs = new FileStream(@"" + error + "(" + DateTime.Now.ToString("MM-dd-yy_HHmm") + ")" + ".txt", FileMode.Create); sw = new StreamWriter(fs); tmp = Console.Out; Console.SetOut(sw); Console.WriteLine("Sorry {0} is invalid or folder/s empty. \n", source); sw.Close(); Console.SetOut(tmp); Console.WriteLine("Sorry {0} is invalid or folder/s empty. \n", source); Console.WriteLine("Please enter source path: " + "(e.g. " + @"C:\foldername\.." + ") \n"); source = Console.ReadLine(); break; } Console.WriteLine("{0} Sorry your input is invalid. \n", error); Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n"); error = Console.ReadLine(); continue; } #endregion continue; } #endregion ascInit.threadWait(); #region while CopyNew Console.WriteLine("\nAsctool version 1.0 "); Console.WriteLine("Apsmith (c) 2014 \n"); Console.WriteLine("Please enter source path: " + "(e.g. " + @"C:\foldername\.." + ") \n"); source = Console.ReadLine(); while (!checkFile) { if ((Directory.Exists(source)) && checkFile == false && ascInit.IsDirectoryEmpty(source) == false) { string sourceDirName2 = @"" + source; Console.WriteLine("\nPlease enter destination path: " + "(e.g. " + @"C:\foldername\.." + ") \n"); string dirSource = Console.ReadLine(); Console.WriteLine("\n"); string destDirName2 = @"" + dirSource; if (Directory.Exists(sourceDirName2)) { DirectoryInfo sourceInfo2 = new DirectoryInfo(sourceDirName2); DirectoryInfo destInfo2 = new DirectoryInfo(destDirName2); ascInit.BackCopy(sourceInfo2, destInfo2); copy_status = "Copy successful."; Console.WriteLine(copy_status); } else { copy_status = "File doesn't exists."; Console.WriteLine(copy_status); } break; } Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n"); error = Console.ReadLine(); #region errorPath while (!checkPath) { if ((File.Exists(error) || Directory.Exists(error)) && checkPath == false) { fs = new FileStream(@"" + error + "(" + DateTime.Now.ToString("MM-dd-yy_HHmm") + ")" + ".txt", FileMode.Create); sw = new StreamWriter(fs); tmp = Console.Out; Console.SetOut(sw); Console.WriteLine("Sorry {0} is invalid or folder/s empty. \n", source); sw.Close(); Console.SetOut(tmp); Console.WriteLine("Sorry {0} is invalid or folder/s empty. \n", source); Console.WriteLine("Please enter source path: " + "(e.g. " + @"C:\foldername\.." + ") \n"); source = Console.ReadLine(); break; } Console.WriteLine("{0} Sorry your input is invalid. \n", error); Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n"); error = Console.ReadLine(); continue; } #endregion continue; } #endregion ascInit.threadWait(); Console.WriteLine("\n"); #region try IIS Start try { ascInit.ExecuteCommandSync("iisreset /start"); } catch (Exception ex) { Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n"); error = Console.ReadLine(); #region errorPath while (!checkPath) { if ((File.Exists(error) || Directory.Exists(error)) && checkPath == false) { fs = new FileStream(@"" + error + "(" + DateTime.Now.ToString("MM-dd-yy_HHmm") + ")" + ".txt", FileMode.Create); sw = new StreamWriter(fs); tmp = Console.Out; Console.SetOut(sw); Console.WriteLine(ex.Message); sw.Close(); Console.SetOut(tmp); Console.WriteLine(ex.Message); break; } Console.WriteLine("{0} Sorry your input is invalid. \n", error); Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n"); error = Console.ReadLine(); continue; } #endregion } #endregion Console.WriteLine("Deployment process complete. " + DateTime.Now.ToString("MM-dd-yy HH:mm")); Environment.Exit(0); } else { dbMessage = "SQL query execution incomplete."; Console.WriteLine(dbMessage); } } catch (Exception ex) { #region errorLogs Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n"); error = Console.ReadLine(); while (!checkPath) { if ((File.Exists(error) || Directory.Exists(error)) && checkPath == false) { fs = new FileStream(@"" + error + "(" + DateTime.Now.ToString("MM-dd-yy_HHmm") + ")" + ".txt", FileMode.Create); sw = new StreamWriter(fs); Console.SetOut(sw); Console.WriteLine(ex.Message); Console.SetOut(tmp); Console.WriteLine(ex.Message); sw.Close(); break; } Console.WriteLine("{0} Sorry your input is invalid. \n", error); Console.WriteLine("\nAn Error Occured.\nPlease enter destination path for logs: \n"); error = Console.ReadLine(); continue; } #endregion } }