I have used below code to download file from FTP server to local file system. But When I used this it shows the following error
I have used the following code
Code Snippet
private string user = "uname";
private string pass = "passwd";
private FtpWebRequest ftpRequest ;
private FtpWebResponse ftpResponse ;
private Stream ftpStream ;
public void download(string remoteFile, string localFile)
{
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create("ftp://uname:passwd@hostname/foldername/filename");
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = false;
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
byte[] byteBuffer = new byte[2048];
int bytesRead = ftpStream.Read(byteBuffer, 0, 2048);
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
bytesRead = ftpStream.Read(byteBuffer, 0, 2048);
}
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
localFileStream.Close();
ftpStream.Close();
ftpResponse.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadKey();
}
return;
}
Can anyone help me in resolving this?
Update
I have also changed ftpRequest.KeepAlive = false;
to ftpRequest.KeepAlive = true;
But I am facing the same exception