private void BtnFileListClick(object sender, RoutedEventArgs e) { try { IFTPAgent ftpAgent = FTPAgentFactory.CreateAgent(FTPAgentFactory.AgentType.Default); string[] list; FtpResponseStatus responseStatus = ftpAgent.NameListOfRemoteDirectory(GetFtpParameters(), String.Empty, out list); if (responseStatus.StatusType == StatusType.Succeeded && list != null) { _lstFiles.Items.Clear(); Array.ForEach(list, fileNameItem => _lstFiles.Items.Add(fileNameItem)); } else { MessageBox.Show(ResponseFormatter.Format(responseStatus)); } } catch (Exception exception) { MessageBox.Show(exception.ToString()); } }
// A quik test for the spell checker public bool DownloadFile(string localFilename, string serverFilename, FtpParameters ftpParameters, out string responseAsString) { #region check arguments if (String.IsNullOrEmpty(localFilename)) { responseAsString = "The local file name is not specified"; return(false); } string directoryName = Path.GetDirectoryName(localFilename); if (String.IsNullOrEmpty(directoryName) || !Directory.Exists(directoryName)) { responseAsString = string.Format("The directory {0} is invalid", directoryName); return(false); } #endregion bool returnValue; const int segmentSize = 16000; FtpResponseStatus response = null; FileStream fileStream = new FileStream(localFilename, FileMode.Create, FileAccess.ReadWrite, FileShare.None); BinaryWriter binaryWriter = new BinaryWriter(fileStream); try { byte[] segment; bool eof = false; int offset = 0; while (!eof) { IFTPAgent gatewayServiceClient = FTPAgentFactory.CreateAgent(_agentType); response = gatewayServiceClient.RetrieveSegment(ftpParameters, serverFilename, offset, segmentSize, out segment, out eof); if (response.StatusType != StatusType.Succeeded) { break; } binaryWriter.Write(segment); offset += segment.Length; } returnValue = eof; } catch (Exception e) // If the request can't be sent correctly, the procy client can generate various exceptions // for exeample if the chunk size is too big , wcf config issue, .... { response = new FtpResponseStatus(); response.StatusType = StatusType.OtherError; response.ErrorDescription = e.ToString(); returnValue = false; } finally { binaryWriter.Close(); } responseAsString = ResponseFormatter.Format(response); return(returnValue); }
public bool UploadFile(string localFilename, string serverFilename, FtpParameters ftpParameters, out string responseAsString) { if (!File.Exists(localFilename)) { responseAsString = string.Format("The file {0} does not exists", localFilename); return(false); } const int chunkSize = 16000; FileInfo fileToSendInfo = new FileInfo(localFilename); long fileSize = fileToSendInfo.Length; FtpResponseStatus lastResponse = null; FileStream fileStream = new FileStream(fileToSendInfo.FullName, FileMode.Open); bool returnValue; try { for (long currentIndex = 0; currentIndex < fileSize; currentIndex += chunkSize) { lastResponse = SendFileChunk(fileStream, chunkSize, ftpParameters, serverFilename); if (lastResponse.StatusType != StatusType.Succeeded) { break; } } responseAsString = ResponseFormatter.Format(lastResponse); } finally { fileStream.Close(); } if (lastResponse == null) { returnValue = false; } else { returnValue = lastResponse.StatusType == StatusType.Succeeded; } return(returnValue); }
private void _btnDelete_Click(object sender, RoutedEventArgs e) { try { IFTPAgent ftpAgent = FTPAgentFactory.CreateAgent(FTPAgentFactory.AgentType.Default); bool existing; FtpResponseStatus responseStatus = ftpAgent.DeleteIfExists(GetFtpParameters(), _tbxFileToDelete.Text, out existing); StringBuilder result = new StringBuilder(); result.AppendLine(ResponseFormatter.Format(responseStatus)); result.AppendLine(String.Format("File was existing : '{0}'", existing)); MessageBox.Show(result.ToString()); } catch (Exception exception) { MessageBox.Show(exception.ToString()); } }