Cannot close the stream from ComponentPro.Net.Ftp.GetDownloadStream

Hello, I want to read just the first few bytes from an ftp to check, if it is the right file, then close that stream and reopen it at position 0 (since setting Stream.Position = 0 will not work) But unfortunately, I cannot close the stream I got from GetDownloadStream. And if I do not close it, anythinge else (like even trying to Diconnect) runs into a FtpException "Cannot execute the operation because the Ftp object is locked by another operation which is still in progress." ComponentPro.Net.Ftp ftp; ftp.Connect... ftp.Authenticate... ... Stream downloadStream = ftp.GetDownloadStream(Filename); byte[] buffer = new byte[10]; int count = downloadStream.Read(buffer, 0, 10); //till here it's working, the 10 bytes are read correctly downloadStream.Close(); // takes a very long time and then throws an InvalidOperationException "No response from the server is expected." downloadStream = ftp.GetDownloadStream(Filename); BTW: This code is running in a background thread What I am doing wrong?
asked 3/30/2018 11:54:42 AM
add a comment

1 Answers

There is a simpler way to do that, please have a look at the DownloadFile method: [,System-IO-Stream,System-Int64,System-Int64)][1]. You can try the following: using (var mem = new MemoryStream()) { ftp.DownloadFile('/remotefile', mem, 0L, 10L); // check your downloaded content here by accessing bytes in the mem stream. } [1]:,System-IO-Stream,System-Int64,System-Int64)
edited 4/2/2018 4:24:31 PM
answered 4/2/2018 8:13:03 AM
add a comment

Your Answer

Not the answer you're looking for? Browse other questions tagged ftp or ask your own question.