public void test() { if (Directory.Exists(workingDirectory)) { Directory.Delete(workingDirectory, true); Directory.CreateDirectory(workingDirectory); } else { Directory.CreateDirectory(workingDirectory); } using (ftp = new FtpConnection(txtServername.Text,21, txtUsername.Text, txtPassword.Text)) { ftp.Open(); /* Open the FTP connection */ ftp.Login(); /* Login using previously provided credentials */ if (ftp.DirectoryExists(txtRootDir.Text)) /* check that a directory exists */ { ftp.SetCurrentDirectory(txtRootDir.Text); /* change current directory */ } // StreamReader reader = new StreamReader(txtBrowse.Text); try { string line = string.Empty; FtpDirectoryInfo info = ftp.GetCurrentDirectoryInfo(); fileInfos = info.GetFiles(); foreach (FtpFileInfo item in fileInfos) { //ftp.GetFile(item.Name, Directory.GetCurrentDirectory() + @"\Output\" + line + @"\item.Name", false); ListboxItem listBoxItem = new ListboxItem(); listBoxItem.fileName = item.Name; listBoxItem.filePath = txtRootDir.Text + item.Name; lstFiles.Items.Add(listBoxItem); } //ftp.SetCurrentDirectory(@"/loth2/itsred/caixa/3.1.22.PROD"); ftp.SetLocalDirectory(workingDirectory); ftp.GetFile(fileInfos[1].Name,false); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { ftp.Close(); } } }
public FileFilter CopyFromFtp(FtpDirectory ftpDir, string destinationDirectory) { using (FtpConnection ftpConnection = new FtpConnection(ftpDir.Host, ftpDir.Port, ftpDir.Username, ftpDir.Password)) { ftpConnection.Open(); ftpConnection.Login(); foreach (string ftpPath in GetFtpFilesAndFolders(ftpDir.BaseDirectory, ftpConnection)) { if (ftpConnection.DirectoryExists(ftpPath)) { var combinedPath = Path.Combine(destinationDirectory, PathWithoutBaseDirectory(ftpPath, ftpDir.BaseDirectory).TrimStart('/')); if (!Directory.Exists(combinedPath)) { Directory.CreateDirectory(combinedPath); } } else { if (!Directory.Exists(destinationDirectory)) { Directory.CreateDirectory(destinationDirectory); } var combinedPath = Path.Combine(destinationDirectory, PathWithoutBaseDirectory(ftpPath, ftpDir.BaseDirectory).TrimStart('/')); var newPath = Path.GetDirectoryName(combinedPath); if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } ftpConnection.GetFile(ftpPath, combinedPath, false); } } ftpConnection.Close(); } return this; }
public FileFilter CopyToFtp(string sourceDirectory, FtpDirectory ftpDir) { using (FtpConnection ftpConnection = new FtpConnection(ftpDir.Host, ftpDir.Port, ftpDir.Username, ftpDir.Password)) { ftpConnection.Open(); ftpConnection.Login(); foreach (WrappedFileSystemInfo fileSystemInfo in GetFilesAndFolders(sourceDirectory)) { if (fileSystemInfo is WrappedDirectoryInfo) { var combinedPath = Path.Combine(ftpDir.BaseDirectory, fileSystemInfo.PathWithoutBaseDirectory); if (!ftpConnection.DirectoryExists(combinedPath)) ftpConnection.CreateDirectory(combinedPath); } else { if (!ftpConnection.DirectoryExists(ftpDir.BaseDirectory)) { ftpConnection.CreateDirectory(ftpDir.BaseDirectory); } var combinedPath = Path.Combine(ftpDir.BaseDirectory, fileSystemInfo.PathWithoutBaseDirectory); var newPath = Path.GetDirectoryName(combinedPath); if (!ftpConnection.DirectoryExists(newPath)) { ftpConnection.CreateDirectory(newPath); } ftpConnection.SetCurrentDirectory(newPath); ftpConnection.PutFile(fileSystemInfo.FullName); } } } return this; }
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 bool IsValidToBeginDiff(string inputEnv, string inputHost, string refEnv, string refHost, string compareScriptLocation, string scriptHost) { FtpConnection connection; #region RefEnvValidation connection = new FtpConnection(refHost, "mxftp", "mxftp"); try { try { connection.Open(); // bgDoServerWork.ReportProgress(5, "Validating Results..."); } catch (Exception ex) { CommonUtils.ShowError(ex.Message, ex); return false; } connection.Login(); if (!connection.DirectoryExists(refEnv)) { CommonUtils.ShowInformation("The reference environment path is incorrect!",true); return false; } if (!connection.FileExists(refEnv + "/mxg2000_settings.sh")) { CommonUtils.ShowInformation("The reference environment path is incorrect!", true); return false; } if (connection.FileExists(refEnv + "/clean.log")) { CommonUtils.ShowInformation("The reference environment is cleaned!", true); return false; } } finally { connection.Close(); connection.Dispose(); } #endregion #region inputEnvValidations connection = new FtpConnection(inputHost, "mxftp", "mxftp"); try { try { connection.Open(); // bgDoServerWork.ReportProgress(5, "Validating Results..."); } catch (Exception ex) { CommonUtils.ShowError(ex.Message, ex); return false; } connection.Login(); if (!connection.DirectoryExists(inputEnv)) { CommonUtils.ShowInformation("The input environment path is incorrect!",true); return false; } if (!connection.FileExists(inputEnv + "/mxg2000_settings.sh")) { CommonUtils.ShowInformation("The input environment path is incorrect!", true); return false; } if (connection.FileExists(inputEnv + "/clean.log")) { CommonUtils.ShowInformation("The input environment is cleaned!", true); return false; } } finally { connection.Close(); connection.Dispose(); } #endregion #region ComparisonHostValidation connection = new FtpConnection(scriptHost, "mxftp", "mxftp"); try { try { connection.Open(); } catch (Exception ex) { CommonUtils.ShowError(ex.Message, ex); return false; } connection.Login(); if (!connection.DirectoryExists(compareScriptLocation)) { CommonUtils.ShowInformation("Could not connect to master host!", true); return false; } if (!connection.FileExists(compareScriptLocation + "/comparison.sh")) { //testing } } finally { connection.Close(); connection.Dispose(); } #endregion return true; }
public void PublishToGitFTP(DeploymentModel model) { if (model.AzureDeployment) { var remoteProcess = Process.Start("\"" + gitLocation + "\"", " --git-dir=\"" + fullRepoPath + "\" remote add blog " + model.AzureRepo); if (remoteProcess != null) remoteProcess.WaitForExit(); var pushProcess = Process.Start("\"" + gitLocation + "\"", " --git-dir=\"" + fullRepoPath + "\" push -f blog master"); if (pushProcess != null) pushProcess.WaitForExit(); } else { using (ftp = new FtpConnection(model.FTPServer, model.FTPUsername, model.FTPPassword)) { try { ftp.Open(); ftp.Login(); if (!string.IsNullOrWhiteSpace(model.FTPPath)) { if (!ftp.DirectoryExists(model.FTPPath)) { ftp.CreateDirectory(model.FTPPath); } ftp.SetCurrentDirectory(model.FTPPath); } FtpBlogFiles(snowPublishPath, model.FTPPath); } catch (Exception ex) { } } } }
private void btnEdit_Click(object sender, EventArgs e) { try { using (ftp = new FtpConnection(txtServername.Text, 21, txtUsername.Text, txtPassword.Text)) { ftp.Open(); /* Open the FTP connection */ ftp.Login(); if (ftp.DirectoryExists(txtRootDir.Text)) /* check that a directory exists */ { ftp.SetCurrentDirectory(txtRootDir.Text); /* change current directory */ } ListboxItem selectedItem = lstFiles.SelectedItem as ListboxItem; ftp.SetLocalDirectory(workingDirectory); ftp.GetFile(selectedItem.fileName,false); StreamReader reader = new StreamReader(workingDirectory + @"\" + selectedItem.fileName,Encoding.Default); txtFileContent.Text = reader.ReadToEnd().Replace("\n","\r\n"); } ftp.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); ftp.Close(); } }