Click here to Skip to main content
11,930,069 members (52,803 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


13 bookmarked

How to check if a file exists on an FTP server

, 1 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Describes how to check for file existence on an FTP Server using VB.NET
This is a method that I’ve used in the past to check for file existence on an FTP server.

Public Function CheckIfFtpFileExists(ByVal fileUri As String) As Boolean
       Dim request As FtpWebRequest = WebRequest.Create(fileUri)
       request.Credentials = New NetworkCredential("username", "password")
       request.Method = WebRequestMethods.Ftp.GetFileSize
           Dim response As FtpWebResponse = request.GetResponse()
           ' THE FILE EXISTS
       Catch ex As WebException
            Dim response As FtpWebResponse = ex.Response
            If FtpStatusCode.ActionNotTakenFileUnavailable = response.StatusCode Then
                ' THE FILE DOES NOT EXIST
                Return False
            End If
        End Try
        Return True
    End Function

Get’s called like this:

If CheckIfFtpFileExists("") Then
        ' Do something
End If


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pete Mourfield
Software Developer (Senior)
United States United States
Software Developer

You may also be interested in...

Comments and Discussions

GeneralExcellent Pin
Samarjeet Singh8-Mar-13 7:04
memberSamarjeet Singh8-Mar-13 7:04 
GeneralC# Version of above code : public bool CheckIfFtpFileExists... Pin
Pravin Patil, Mumbai1-Mar-11 22:33
memberPravin Patil, Mumbai1-Mar-11 22:33 
GeneralReason for my vote of 5 Interesting solution. I would sugges... Pin
DrABELL1-Mar-11 10:59
memberDrABELL1-Mar-11 10:59 
GeneralDoes not work Pin
us47118-Mar-11 2:23
memberus47118-Mar-11 2:23 
I tried the solution, and got the error
I checked the URI and the existance of the requested file on the server, the correct spelling of the URI (with upper-lowercase, too), but were not successfull.
By the way: Visual Studio 2010 with .NET framework 3.5

Any idea? Thanks for your help.
GeneralRe: Does not work Pin
Francis Lamarre19-Jan-12 9:24
memberFrancis Lamarre19-Jan-12 9:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 1 Mar 2011
Article Copyright 2011 by Pete Mourfield
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid