/// <summary> /// Sterge o imagine de pe contul FTP. /// </summary> /// <param name="imgName">Numele imaginii de pe contul FTP.</param> /// <returns>True, daca s-a sters si False daca a aparut o eroare.</returns> public static bool DeleteImage(string imgName) { try { FtpConnection conn = new FtpConnection(Globals.FTP_SERVER, 21, Globals.FTP_USER, Globals.FTP_PASSWORD); conn.Open(); conn.Login(); conn.SetCurrentDirectory("/public_html/CompanyWorkManagement/"); conn.RemoveFile(imgName + ".jpg"); return true; } catch { return false; } }
public static bool DeleteImage(string imgName) { try { FtpConnection conn = new FtpConnection(ftpServer, 21, ftpUser, ftpPassword); conn.Open(); conn.Login(); conn.SetCurrentDirectory("/ion"); conn.RemoveFile(imgName + ".jpg"); return true; } catch { return false; } }
public void RecursiveDeleteFTP(string path, bool RemFromLog, ref FtpConnection ftpcon) { ftpcon.SetCurrentDirectory(path); foreach (FtpFileInfo fi in ftpcon.GetFiles()) { string fpath = string.Format("{0}/{1}", path, fi.Name); ftpcon.RemoveFile(fpath); Log.Write("Gon'delete: {0}", fpath); if (RemFromLog) RemoveFromLog(fpath); } foreach (FtpDirectoryInfo di in ftpcon.GetDirectories()) { if (di.Name != "." && di.Name != "..") { string fpath = string.Format("{0}/{1}", noSlashes(path), di.Name); Log.Write("Gon'delete files in: {0}", fpath); RecursiveDeleteFTP(fpath, RemFromLog, ref ftpcon); } } ftpcon.RemoveDirectory(path); if (RemFromLog) RemoveFromLog(path); }
private static void RemoveDirectoryEvenIfNotEmpty(FtpConnection ftpConnection, string startDirectory) { if (!startDirectory.EndsWith("/")) startDirectory += "/"; List<string> directories = new List<string>(); directories.Add(startDirectory); int index = 0; while (index < directories.Count) { string currentDirectory = directories[index]; directories.AddRange(ftpConnection.GetDirectories(currentDirectory).Select(dir => currentDirectory + dir.Name + "/")); index++; } directories.Reverse(); List<string> fileNames = new List<string>(); foreach (var directory in directories) { foreach (var ftpFile in ftpConnection.GetFiles(directory)) { string fullPath = directory + ftpFile.Name; if (ftpConnection.FileExists(fullPath)) ftpConnection.RemoveFile(fullPath); } foreach (var ftpDir in ftpConnection.GetDirectories(directory)) { string fullPath = directory + ftpDir.Name; if (ftpConnection.DirectoryExists(fullPath)) ftpConnection.RemoveDirectory(fullPath); } } if (startDirectory != "./" && ftpConnection.DirectoryExists(startDirectory)) ftpConnection.RemoveDirectory(startDirectory); }
public void DeleteFromFtp(FtpDirectory ftpDir) { using (FtpConnection ftpConnection = new FtpConnection(ftpDir.Host, ftpDir.Port, ftpDir.Username, ftpDir.Password)) { ftpConnection.Open(); ftpConnection.Login(); var paths = GetFtpFilesAndFolders(ftpDir.BaseDirectory, ftpConnection); paths.Reverse(); foreach (var path in paths) { var fullPath = ftpDir.BaseDirectory + path; if (ftpConnection.DirectoryExists(fullPath)) RemoveDirectoryEvenIfNotEmpty(ftpConnection, fullPath); else { ftpConnection.RemoveFile(fullPath); } } ftpConnection.Close(); } }
private void StartComparison(string inputEnv, string inputHost, string refEnv, string refHost, string compareScriptLocation, out DataTable resultsTable) { resultsTable = new DataTable(); string scriptHost = "dell014srv"; bgDoServerWork.ReportProgress(5, "Validating environments..."); if (LOCAL_TA_USAGE || IsValidToBeginDiff(inputEnv, inputHost, refEnv, refHost, compareScriptLocation, scriptHost)) { bgDoServerWork.ReportProgress(5, "Connecting to Hosts..."); if (bgDoServerWork.CancellationPending) { WorkerWasCancelled = true; return; } //CopyComparisonScript(refHost, refEnv); SshStream ssh = new SshStream(scriptHost, "autoengine", ""); //Set the end of response matcher character string response = string.Empty; try { ssh.Prompt = "\\$"; //Remove terminal emulation characters ssh.RemoveTerminalEmulationCharacters = true; //Writing to the SSH channel //tmReturn.Start(); //Remove terminal emulation characters ssh.RemoveTerminalEmulationCharacters = true; //Writing to the SSH channel ssh.Write("cd " + compareScriptLocation); //Reading from the SSH channel response = ssh.ReadResponse(); } catch (Exception ex) { CommonUtils.LogError(ex.Message, ex); } string comparisonFolderName = DateTime.Now.Ticks.ToString(); string comparisonFileName = comparisonFolderName + "comparison.log"; if (bgDoServerWork.CancellationPending) { WorkerWasCancelled = true; return; } try { ssh.Write("mkdir " + comparisonFolderName); response = ssh.ReadResponse(); ssh.Write("cp comparison88.sh " + comparisonFolderName); response = ssh.ReadResponse(); ssh.Write("cd " + comparisonFolderName); //Reading from the SSH channel response = ssh.ReadResponse(); ssh.Write("comparison88.sh " + "/net/" + refHost + refEnv + " /net/" + inputHost + inputEnv + " -A >" + comparisonFileName); bgDoServerWork.ReportProgress(5, "Starting Comparison..."); response = ssh.ReadResponse(); ssh.Write("d"); response = ssh.ReadResponse(); } catch (Exception ex) { CommonUtils.LogError(ex.Message, ex); } bgDoServerWork.ReportProgress(5, "Comparison Completed"); if (bgDoServerWork.CancellationPending) { WorkerWasCancelled = true; return; } FtpConnection connection = new FtpConnection(scriptHost, "mxftp", "mxftp"); string localFileName = Path.GetTempFileName(); try { try { try { connection.Open(); bgDoServerWork.ReportProgress(5, "Validating Results..."); } catch (Exception ex) { CommonUtils.ShowError(ex.Message, ex); } connection.Login(); connection.SetCurrentDirectory(compareScriptLocation + comparisonFolderName); //Path.GetTempFileName connection.GetFile(comparisonFileName, localFileName, false); connection.RemoveFile(comparisonFileName); connection.SetCurrentDirectory(compareScriptLocation); bgDoServerWork.ReportProgress(5, "Building Output..."); } finally { connection.Close(); connection.Dispose(); } } catch (Exception ex) { CommonUtils.LogError(ex.Message, ex); } try { ssh.Write("cd .."); //Reading from the SSH channel response = ssh.ReadResponse(); ssh.Write("rm -rf " + comparisonFolderName); //Reading from the SSH channel response = ssh.ReadResponse(); ssh.Flush(); ssh.Close(); ssh.Dispose(); if (bgDoServerWork.CancellationPending) { WorkerWasCancelled = true; return; } } catch (Exception ex) { CommonUtils.LogError(ex.Message, ex); } try { string readFile = CommonUtils.ReadFile(localFileName); // Regex regex = new Regex("-.*-"); string[] array = new string[1]; // array[0]= regex.Match(readFile).Value; array[0] = "\n\n"; string[] splitFile = readFile.Split(array, StringSplitOptions.RemoveEmptyEntries); int counter = 1; bgDoServerWork.ReportProgress(5, "Displaying Results..."); Regex diffRegex = new Regex("diff.*"); DataTable results = new DataTable(); results.Columns.Add("Number"); results.Columns.Add("Operation"); results.Columns.Add("FileSize"); results.Columns.Add("FileModifyDate"); results.Columns.Add("FileType"); results.Columns.Add("FileName"); List<string> monthAbbreviations = new List<string>() { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; for (int j = 2; j < splitFile.Length; j++) { string[] splitByLines = splitFile[j].Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); for (int k = 3; k < splitByLines.Length; k++) { //-rw-rw-r-- 1 autoengine murex 1201 Jul 10 16:58 test.txt //-rwxrwxr-x 1 autoengine murex 546 May 10 2008 script.sh* // "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", and "Dec" if (j == 2) { string fileName = splitByLines[k].Replace("/net/" + refHost + refEnv, "").Replace("/net/" + inputHost + inputEnv, ""); string[] splitFileName = fileName.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (diffRegex.Matches(fileName).Count == 0) { DateTime date; try { if (splitFileName[7].Contains(":")) { date = new DateTime(DateTime.Now.Year, monthAbbreviations.IndexOf(splitFileName[5]), Convert.ToInt32(splitFileName[6]), Convert.ToInt32(splitFileName[7].Split(new char[] { ':' })[0]), Convert.ToInt32(splitFileName[7].Split(new char[] { ':' })[1]), 0); } else { date = new DateTime(Convert.ToInt32(splitFileName[7]), monthAbbreviations.IndexOf(splitFileName[5]), Convert.ToInt32(splitFileName[6])); } } catch (Exception ex) { CommonUtils.LogError("Could not get Date", ex); date = DateTime.Now; } long fileSize = (Convert.ToInt64(splitFileName[4]) / 1024) == 0 ? 1 : (Convert.ToInt64(splitFileName[4]) / 1024); results.Rows.Add(new object[] { counter, "Added", fileSize, date.ToString(), GetFileTypeFromExtension(splitFileName[8]), splitFileName[8] }); } } else { string fileName = splitByLines[k].Replace("/net/" + refHost + refEnv, "").Replace("/net/" + inputHost + inputEnv, ""); string[] splitFileName = fileName.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (diffRegex.Matches(fileName).Count == 0) { DateTime date; try { if (splitFileName[7].Contains(":")) { date = new DateTime(DateTime.Now.Year, monthAbbreviations.IndexOf(splitFileName[5]), Convert.ToInt32(splitFileName[6]), Convert.ToInt32(splitFileName[7].Split(new char[] { ':' })[0]), Convert.ToInt32(splitFileName[7].Split(new char[] { ':' })[1]), 0); } else { date = new DateTime(Convert.ToInt32(splitFileName[7]), monthAbbreviations.IndexOf(splitFileName[5]), Convert.ToInt32(splitFileName[6])); } } catch (Exception ex) { CommonUtils.LogError("Could not get Date", ex); date = DateTime.Now; } long fileSize = (Convert.ToInt64(splitFileName[4]) / 1024) == 0 ? 1 : (Convert.ToInt64(splitFileName[4]) / 1024); results.Rows.Add(new object[] { counter, "Modified", fileSize, date.ToString(), GetFileTypeFromExtension(splitFileName[8]), splitFileName[8] }); } } counter++; } } SAVED_SEARCH_RESULTS = results.Copy(); GOLDEN_ORIGINAL_RESTULS = results.Copy(); resultsTable = SAVED_SEARCH_RESULTS; } catch (Exception ex) { CommonUtils.LogError(ex.Message, ex); } } else { bgDoServerWork.CancelAsync(); WorkerWasCancelled = true; } }