开发者

Checking if file exist or not on ftp server before downloading it

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.

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 > 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");
                     Sy开发者_JAVA百科stem.Diagnostics.Debug.WriteLine("Error: " + fileName + " is not available on fpt server");
                 }
            }   
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Error: " + ex.Message);
            }
        }


My understanding is that you have to create a new FtpWebRequest for each request you make. So before setting the Method again you'd have to create a new one and set the credentials again. So pretty much that you'd have to repeat the following two lines:

ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath + fileName));
ftpRequest.Credentials = new NetworkCredential(userName, password);


When you connect to an FTP server you might specify the Uri as "ftp//ftp.domain.com/somedirectory" but this translates to: "ftp://ftp.domain.com/homedirectoryforftp/somedirectory". To be able to define the full root directory use "ftp://ftp.domain.com//somedirectory" which translates to //somedirectory on the machine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜