Checking if file exist or not on ftp server before downloading it
<p>Before downloading a file from ftp server, I want to check it if exist or not, if not it mu
st throw exception. The code sample works when the file does not exist. However when the file exist, after execution the line; "ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;" it jumps the second catch block and prints "Error: This operation cannot be performed after the request has been submitted." what's the point that i can't see..Thanks for answers.</p>

<pre><code>public void fileDownload(string fileName)
        {
            stream = new FileStream(filePath + fileName, FileMode.Create);
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName));
            ftpRequest.Credentials = new NetworkCredential(userName, password);
            ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;

            try
            {
                response = (FtpWebResponse)ftpRequest.GetResponse();
                ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                ftpRequest.UseBinary = true;
                response = (FtpWebResponse)ftpRequest.GetResponse();
                ftpStream = response.GetResponseStream();
                cl = response.ContentLength;
                bufferSize = 2048;
                buffer = new byte[bufferSize];
                readCount = ftpStream.Read(buffer, 0, bufferSize);

                while (readCount &gt; 0)
                {
                    stream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }

                ftpStream.Close();
                stream.Close();
                response.Close();
                Console.WriteLine("File : " + fileName + " is downloaded from ftp server"); 
            }
            catch (WebException ex)
            {
                 FtpWebResponse res = (FtpWebResponse)ex.Response;
                 if (res.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                 {
                     stream.Close();
                     File.Delete(filePath + fileName);
                     Console.WriteLine("File : " + fileName + " does not exists on ftp server");
                     System.Diagnostics.Debug.WriteLine("Error: " + fileName + " is not available on fpt server");
                 }
            }   
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Error: " + ex.Message);
            }
        }
</code></pre>
