1

I have got a code to download a file using ftprequest

FtpWebRequest requestFileDownload = (FtpWebRequest)WebRequest.Create("ftp://localhost/Source/" + fileName);
requestFileDownload.Credentials = new NetworkCredential("khanrahim", "arkhan22");
requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;

FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();

Stream responseStream = responseFileDownload.GetResponseStream();
FileStream writeStream = new FileStream(localPath + fileName, FileMode.Create);  

int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);               

while (bytesRead > 0)
{
  writeStream.Write(buffer, 0, bytesRead);
  bytesRead = responseStream.Read(buffer, 0, Length);
}   

responseStream.Close();             
writeStream.Close();

Now my need is that i need to delete the file from ftpserver once download is complete using the same requset.

I did try appending requestFileDownload.Method = WebRequestMethods.Ftp.deleteFile; before closing the request ..But it is not working. How can i delete the file using the same request.

user2799127
  • 113
  • 2
  • 9
  • after you appended that did you execute it with `FtpWebResponse responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();` ? just a guess but maybe that will do it – Joe T Dec 19 '13 at 05:13
  • s i did do that but i got an error lik "cannot do this after server req is complte" – user2799127 Dec 19 '13 at 05:45

1 Answers1

3

Objects created by WebRequest.Create can be used for exactly one request. Since there is no "GET and DELETE" method in FTP you need to create another FtpWebRequest with the same configuration and send delete request with that new FtpWebRequest.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • In that case how can i avoid re login – user2799127 Dec 19 '13 at 05:46
  • @user2799127 - you can't - with `FtpWebRequest` you can't issue 2 commands and have to re-login for every request. If you need to talk to the same server with multiple requests - consider [searching for full FTP client](http://stackoverflow.com/questions/1371964/free-ftp-library). – Alexei Levenkov Dec 19 '13 at 16:49