locked
How to check directory Exists in Ftp Server

    Question

  • please help me,
    i would like to uplaod a file to Ftp server ,before doing this 
    i have to check some directory exists .how is it possible
    //
    suppose  test.txt is the file to upload  to ftp://ip/folder1/test.text
    i have to check folder1 exists 

    with advance thanks
    Monday, September 22, 2008 5:48 AM

Answers

  • I assume that you are already somewhat familiar with FtpWebRequest, as this is the usual way to access FTP in .NET.

    You can attempt to list the directory and check for an error StatusCode.

    try 
    {  
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.microsoft.com/12345");  
        request.Method = WebRequestMethods.Ftp.ListDirectory;  
        using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())  
        {  
            // Okay.  
        }  
    }  
    catch (WebException ex)  
    {  
        if (ex.Response != null)  
        {  
            FtpWebResponse response = (FtpWebResponse)ex.Response;  
            if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)  
            {  
                // Directory not found.  
            }  
        }  
    • Marked as answer by jabi Tuesday, September 23, 2008 6:30 AM
    • Marked as answer by jabi Tuesday, September 23, 2008 6:31 AM
    Monday, September 22, 2008 11:33 PM

All replies

  • I assume that you are already somewhat familiar with FtpWebRequest, as this is the usual way to access FTP in .NET.

    You can attempt to list the directory and check for an error StatusCode.

    try 
    {  
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.microsoft.com/12345");  
        request.Method = WebRequestMethods.Ftp.ListDirectory;  
        using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())  
        {  
            // Okay.  
        }  
    }  
    catch (WebException ex)  
    {  
        if (ex.Response != null)  
        {  
            FtpWebResponse response = (FtpWebResponse)ex.Response;  
            if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)  
            {  
                // Directory not found.  
            }  
        }  
    • Marked as answer by jabi Tuesday, September 23, 2008 6:30 AM
    • Marked as answer by jabi Tuesday, September 23, 2008 6:31 AM
    Monday, September 22, 2008 11:33 PM
  • Hi Bc,

    Apologies for posting on an old thread...

    I'm having a go with this code and with inputting a known folder as a test, it returns not found with the error: "The underlying connection was closed: An unexpected error occurred on a receive."

    I have used the same file path in different code (copying a file using ftp), and it works fine. Any ideas?

    Many Thanks

    Wednesday, October 07, 2009 2:32 PM
  • Hi All,

    I checked all the above but nothing works so I tried my own and finally got the way and it works too fast. Neet to test then test this:

     

            private bool FtpDirectoryExists(string dirPath)
            {
                bool IsExists = true;
                try
                {
                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(dirPath);
                    request.Credentials = new NetworkCredential(FTPUser, FTPPassword);
                    request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;

                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();              
                }
                catch (WebException ex)
                {
                    IsExists = false;
                }
                return IsExists;
            }

     

    Thanks Ali Adravi

    www.Metaoption.com

    Monday, July 26, 2010 7:06 AM
  • Hi Ali,

      I have tried your qpproach but it was not working. I am getting the follwing error.

                  "The remote server returned an error: (530) Not logged in."

    I am passing authenticated user name and password only. even it throwing this error. please advise me.

     

     

    Friday, July 01, 2011 1:59 PM
  • Hi,

      Can anyone help me? pls

    Wednesday, July 06, 2011 1:50 PM
  • I have tried this one .. it is working fine for me...

    public bool FtpDirectoryExists(string directoryPath, string ftpUser, string ftpPassword)
            {
                bool IsExists = true;
                try
                {
                    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(directoryPath);
                    request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                    request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;
    
                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                }
                catch (WebException ex)
                {
                    IsExists = false;
                }
                return IsExists;
            }

    I have called this method as:

    bool result =    FtpActions.Default.FtpDirectoryExists( @"ftp://mydomain.com/abcdir", txtUsername.Text, txtPassword.Text);



    -Niranjan Kala

    • Edited by Niranjan Kala Monday, June 18, 2012 5:43 AM formatting changed
    Wednesday, July 20, 2011 10:29 AM
  • Dear Niranjan,

    I use your code but it gives an error on:

    bool result =    FtpActions.Default

    so i want to know that what is "FtpActions.Default"...........................Do i need to add some namespace so that it' work or not???????????Please tell me i am waiting for your promp reply.

    Thanks,

    Monday, February 13, 2012 3:19 AM
  • Hi Try this code with a slight modification, it worked for me

    Trick is to use WebRequestMethods.Ftp.ListDirectory

    directory is the fullFtpPath i.e. ftp://my.ftp.com/root/newDirectory, where you want to check newDirecotry exists or not?

    private static bool FtpDirectoryExists(string directory, string username, string password)
            {
                try
                {
                    var request = (FtpWebRequest)WebRequest.Create(directory);
                    request.Credentials = new NetworkCredential(username, password);
                    request.Method = WebRequestMethods.Ftp.ListDirectory;


                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                }
                catch (WebException ex)
                {
                    FtpWebResponse response = (FtpWebResponse)ex.Response;
                    if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                        return false;
                    else
                        return true;
                }
                return true;

            }

    Thursday, March 08, 2012 10:59 PM
  • I almost tried all of these.. ListDirectory, PrintWorkingDirectory but none of these seem to throw a 550 Error.. Not sure why this happens.. Any updates to the way in which FTP functions?.. Any new methods of having this solved?...Please do let me know, Thank you All...

    Wednesday, October 17, 2012 1:55 AM