How to check that directory exist on ftp server or not?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aamersaeed2368
    New Member
    • Dec 2011
    • 13

    #1

    How to check that directory exist on ftp server or not?

    Dear All,

    I want to write a function of bool type to check whether a directory exist on ftp server or not..........I' ve googled it so much but couldn't get the solution......C urrently i am using
    Code:
    WebRequestMethods.Ftp.GetDateTimestamp;
    but this function return false in both cases either directory exist or not............ ..And another function
    Code:
    WebRequestMethods.Ftp.PrintWorkingDirectory;
    which return true for every case either directory exist or not??????Can anyone point me in the right direction.

    Thanks,
    Aamer.
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    Here's one option you have for solving your issue using the FtpWebRequest and FtpWebResponse classes built into the .NET Framework

    Code:
    public bool DoesExist(string file)
    {
    	var ftpRequest = (FtpWebRequest)WebRequest.Create (string.Format("{0}/{1}","ftp://ftp.domain.com",file));
    	ftpRequest.Credentials = new NetworkCredential("username", "password");
    	ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
    	
    	try
    	{
    	    FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
    	    return true;
    	}
    	catch (WebException ex)
    	{
    	    FtpWebResponse ftpResponse = (FtpWebResponse)ex.Response;
    	    if (ftpResponse.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
    	    {
    	        return false;
    	    }
    	}
    }

    Comment

    Working...