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(); } } }
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(); } }
private void SendFileToServer(string filename) { List<string> remoteFiles = new List<string>(); string localDownloadDirectory = string.Empty; FtpConnection connection = new FtpConnection("dell014srv", "mxftp", "mxftp"); try { try { connection.Open(); } catch (Exception ex) { CommonUtils.ShowError(ex.Message, ex); } connection.Login(); connection.SetCurrentDirectory("/dell014srv1/automation/automation-Jobs/"); connection.PutFile(filename, "launchJob.sh"); } finally { connection.SetLocalDirectory(Path.GetTempPath()); connection.Close(); connection.Dispose(); } }
private void GetFilesByFtp(string host, string remoteLocation) { List<string> remoteFiles = new List<string>(); string localDownloadDirectory = string.Empty; FtpConnection connection = new FtpConnection(host, "mxftp", "mxftp"); try { try { connection.Open(); } catch (Exception ex) { CommonUtils.ShowError(ex.Message, ex); } connection.Login(); connection.SetCurrentDirectory(remoteLocation); FtpFileInfo[] ftpFileInfo = connection.GetFiles("*act.rftvp"); ScriptName = connection.GetCurrentDirectoryInfo().Name; localDownloadDirectory = downloadDir + ScriptName; //if(Directory.Exists(localDownloadDirectory)){ // Directory.Delete(localDownloadDirectory,true); // } DirectoryInfo downloadDirectoryInfo = Directory.CreateDirectory(localDownloadDirectory); connection.SetLocalDirectory(downloadDirectoryInfo.FullName); for (int i = 0; i < ftpFileInfo.Length; i++) { connection.GetFile(ftpFileInfo[i].Name, false); } } finally { connection.SetLocalDirectory(Path.GetTempPath()); connection.Close(); connection.Dispose(); } ReplaceFiles(localDownloadDirectory, txtDest.Text); //deleting local files //Directory.Delete(localDownloadDirectory); }
private void DownloadFileList(List<string> fileList, string remoteHost, string remoteLocation, string localLocation) { FtpConnection connection = new FtpConnection(remoteHost, "mxftp", "mxftp"); string localFileName = Path.GetTempFileName(); string currentProcessedFile = string.Empty; try { try { try { connection.Open(); } catch (Exception ex) { CommonUtils.ShowError(ex.Message, ex); } connection.Login(); connection.SetCurrentDirectory(remoteLocation); //Path.GetTempFileName connection.SetLocalDirectory(localLocation); for (int i = 0; i < fileList.Count; i++) { currentProcessedFile = fileList[i]; string localDownloadPath = string.Empty; if (fileList[i].StartsWith("/")) { localDownloadPath = Path.GetDirectoryName(localLocation + fileList[i]); } else { localDownloadPath = Path.GetDirectoryName(localLocation + "/" + fileList[i]); } if (!Directory.Exists(localDownloadPath)) { Directory.CreateDirectory(localDownloadPath); } connection.GetFile(fileList[i], false); } } finally { connection.Close(); connection.Dispose(); } } catch (Exception ex) { CommonUtils.ShowError("Stopped @"+currentProcessedFile+"!\n"+ex.Message, ex); } }