public void GetFile(string path, MemoryStream memStream) { lock (commandLock) { FtpResponseCollection response = null; try { CheckConnected(); // replace the windows style directory delimiter with a unix style delimiter path = NormaliseForFTP(path); FtpRequest request = new FtpRequest(FtpRequest.BuildCommandText("RETR", ftpInstance.CharacterEncoding, new string[] { path })); ftpInstance.TransferData(TransferDirection.ToClient, request, memStream); response = ftpInstance.LastResponseList; } catch { if ((response == null) && (memStream.Position != memStream.Length) ) { // This means that the expected read of the fragment did not complete and aborted for another reason throw; } } } }
private FtpResponseCollection Quote(byte[] preEncodedCommand, bool useNewResponseList = false) { try { FtpResponseCollection response = null; FtpRequest request = new FtpRequest(preEncodedCommand); if (happyCodes != null) { request.HappyCodes = happyCodes; } if (useNewResponseList) { response = new FtpResponseCollection(ftpInstance.TransferText(request)); } else { ftpInstance.SendRequest(request); response = ftpInstance.LastResponseList; } return(response); } finally { happyCodes = null; } }
private void Parse(FtpResponseCollection dirResults) { foreach (FileSystemFTPInfo item in dirResults.Select(response => ParseLine(response.RawText)) .Where(item => (item != null) && (item.Name != ".") && (item.Name != "..")) ) { Add(item); } }
/// <summary> /// Split a multi-line file list text response and add the parsed items to the collection. /// </summary> /// <param name="ftpCmdInstance"></param> /// <param name="path">Path to the item on the FTP server.</param> /// <param name="dirResults">The multi-line file list text from the FTP server.</param> public MlstCollection(FtpClientExt ftpCmdInstance, string path, FtpResponseCollection dirResults) { if (ftpCmdInstance == null) { throw new ArgumentNullException("ftpCmdInstance"); } this.ftpCmdInstance = ftpCmdInstance; this.path = path; Parse(dirResults); }
/// <summary> /// Get as many details as it can about the target. /// </summary> /// <param name="target">If empty then will assume CWD has been used</param> /// <returns>May return null if nothing found</returns> public FileSystemFTPInfo GetFileDetails(string target) { List <FileSystemFTPInfo> foundValues; lock (commandLock) { // replace the windows style directory delimiter with a unix style delimiter target = NormaliseForFTP(target); Features featureToUse = ((SupportedFeatures & Features.MLST) == Features.MLST) ? Features.MLST : Features.LIST; if (featureToUse == Features.MLST) { happyCodes = FtpRequest.BuildResponseArray(FtpResponseCode.RequestedFileActionOkayAndCompleted, FtpResponseCode.SyntaxErrorInParametersOrArguments // Stop using exceptions to detect missing ); } else { happyCodes = FtpRequest.BuildResponseArray(FtpResponseCode.DataConnectionAlreadyOpenSoTransferStarting, FtpResponseCode.FileStatusOkaySoAboutToOpenDataConnection, FtpResponseCode.ClosingDataConnection, FtpResponseCode.RequestedFileActionOkayAndCompleted ); } FtpResponseCollection dirResults = (string.IsNullOrEmpty(target)) ? Feature((featureToUse != Features.MLST), featureToUse) : Feature((featureToUse != Features.MLST), featureToUse, true, target); if (featureToUse == Features.MLST) { foundValues = new MlstCollection(this, target, dirResults); } else { // Do it the harder way ?? FtpItemCollection results = new FtpItemCollection(target, dirResults.GetRawText(), ftpInstance.ItemParser); foundValues = FileSystemFTPInfo.ConvertFrom(results, this); } } return(foundValues.Count > 0 ? foundValues[0] : null); }
//public bool ChangeDirectory(string path) //{ // if (path == null) // throw new ArgumentNullException("path"); // if (path.Length == 0) // throw new ArgumentException("must have a value", "path"); // // replace the windows style directory delimiter with a unix style delimiter // path = NormaliseForFTP( path ); // lock (commandLock) // { // FtpRequest request = new FtpRequest(FtpCmd.Cwd, ftpInstance.CharacterEncoding, path) // { // HappyCodes = // FtpRequest.BuildResponseArray( // FtpResponseCode.RequestedFileActionOkayAndCompleted, // FtpResponseCode.RequestedActionNotTakenFileUnavailable // ) // }; // CheckConnected(); // ftpInstance.SendRequest(request); // return ftpInstance.LastResponse.Code == FtpResponseCode.RequestedFileActionOkayAndCompleted; // } //} public List <FileSystemFTPInfo> GetDirList(string path) { List <FileSystemFTPInfo> foundValues; lock (commandLock) { // replace the windows style directory delimiter with a unix style delimiter path = NormaliseForFTP(path); Features featureToUse = ((SupportedFeatures & Features.MLSD) == Features.MLSD) ? Features.MLSD : Features.LIST; if (featureToUse == Features.MLSD) { happyCodes = FtpRequest.BuildResponseArray(FtpResponseCode.ClosingDataConnection, FtpResponseCode.RequestedFileActionOkayAndCompleted); } else { happyCodes = FtpRequest.BuildResponseArray(FtpResponseCode.DataConnectionAlreadyOpenSoTransferStarting, FtpResponseCode.FileStatusOkaySoAboutToOpenDataConnection, FtpResponseCode.ClosingDataConnection, FtpResponseCode.RequestedFileActionOkayAndCompleted); } FtpResponseCollection dirResults = (string.IsNullOrEmpty(path)) ? Feature((featureToUse != Features.MLSD), featureToUse) : Feature((featureToUse != Features.MLSD), featureToUse, true, path); if (featureToUse == Features.MLSD) { foundValues = new MlstCollection(this, path, dirResults); } else { // Do it the harder way ?? FtpItemCollection results = new FtpItemCollection(path, dirResults.GetRawText(), ftpInstance.ItemParser); foundValues = FileSystemFTPInfo.ConvertFrom(results, this); } } return(foundValues); }