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); }
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()); } }
private FtpResponseStatus SendFileChunk(Stream fileStream, int chunkSize, FtpParameters ftpParameters, string serverFilename) { FtpResponseStatus returnValue; try { IFTPAgent ftpAgent = FTPAgentFactory.CreateAgent(_agentType); byte[] tempBuffer = new byte[chunkSize]; byte[] bytesToSend; int numberOfBytesTosend = fileStream.Read(tempBuffer, 0, chunkSize); if (numberOfBytesTosend < chunkSize) { bytesToSend = new byte[numberOfBytesTosend]; Array.Copy(tempBuffer, bytesToSend, numberOfBytesTosend); } else { bytesToSend = tempBuffer; } returnValue = ftpAgent.AppendFile(ftpParameters, serverFilename, bytesToSend); return(returnValue); } 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, .... { returnValue = new FtpResponseStatus(); returnValue.StatusType = StatusType.OtherError; returnValue.ErrorDescription = e.ToString(); } return(returnValue); }