How to check if a file exists on FTP before FtpWebRequest

I need to use FtpWebRequest to put a file in an FTP directory. Before downloading, I would like to first find out if this file exists.

Which method or property should be used to check for the presence of this file?

+61
c # ftp ftpwebrequest
Dec 07 '08 at 18:44
source share
4 answers
 var request = (FtpWebRequest)WebRequest.Create ("ftp://ftp.domain.com/doesntexist.txt"); request.Credentials = new NetworkCredential("user", "pass"); request.Method = WebRequestMethods.Ftp.GetFileSize; try { FtpWebResponse response = (FtpWebResponse)request.GetResponse(); } catch (WebException ex) { FtpWebResponse response = (FtpWebResponse)ex.Response; if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) { //Does not exist } } 

This is usually a bad idea to use Exceptions for functionality in your code like this, but in this case I consider this a victory for pragmatism. A list of calls in a directory may be more inefficient than using exceptions in this way.

If you do not, just know that this is not good practice!

EDIT: "This works for me!"

This seems to work on most ftp servers, but not all. Some servers require sending "TYPE I" before the SIZE command will work. It would seem that the problem should be solved as follows:

 request.UseBinary = true; 

Unfortunately, this is a design limitation (a big fat mistake!), If FtpWebRequest doesn’t upload or download the file, it DOES NOT send "TYPE I". See Microsoft Discussion and Answer here .

Instead, I recommend using the following WebRequestMethod, this works for me on all tested servers, even those that do not return file size.

 WebRequestMethods.Ftp.GetDateTimestamp 
+103
Dec 07 '08 at 23:40
source share

Insofar as

 request.Method = WebRequestMethods.Ftp.GetFileSize 

a crash may occur in some cases (550: SIZE is not allowed in ASCII mode), you can simply check the Timestamp instead.

 reqFTP.Credentials = new NetworkCredential(inf.LogOn, inf.Password); reqFTP.UseBinary = true; reqFTP.Method = WebRequestMethods.Ftp.GetDateTimestamp; 
+6
Sep 16 '15 at 15:39
source share

FtpWebRequest (or any other class in .NET) does not have an explicit method for checking the existence of a file. You must abuse the request like GetFileSize or GetDateTimestamp .

 string url = "ftp://ftp.example.com/remote/path/file.txt"; WebRequest request = WebRequest.Create(url); request.Credentials = new NetworkCredential("username", "password"); request.Method = WebRequestMethods.Ftp.GetFileSize; try { request.GetResponse(); Console.WriteLine("Exists"); } catch (WebException e) { FtpWebResponse response = (FtpWebResponse)e.Response; if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) { Console.WriteLine("Does not exist"); } else { Console.WriteLine("Error: " + e.Message); } } 



If you need simpler code, use some third-party FTP library.

For example, using the WinSCP.NET assembly, you can use your Session.FileExists method :

 SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Ftp, HostName = "ftp.example.com", UserName = "username", Password = "password", }; Session session = new Session(); session.Open(sessionOptions); if (session.FileExists("/remote/path/file.txt")) { Console.WriteLine("Exists"); } else { Console.WriteLine("Does not exist"); } 

(I am the author of WinSCP)

+1
Apr 27 '18 at 6:52
source share

I am using FTPStatusCode.FileActionOK to check if a file exists ...

then in the "else" section, return false.

-one
Dec 09 '17 at 9:51 on
source share



All Articles