tags:

views:

233

answers:

3

Hi, how to check if some dir exists on the server or not. Although I can check file exists or not through

try { FtpWebRequest request=null;

                request = (FtpWebRequest)WebRequest.Create("ftp://" + webrequestUrl + "/somefile.txt");
            request.Credentials = new NetworkCredential(username, password);
            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)
                {
                   //task
                }
            }
        }

But how to check DIR. if I only specify DIR in URI then it doesn't go to catch if di don't exists.

A: 

I don't think the code does what you think it does. As far as I understand the docs you're trying to get a ls (think dir in DOS/Windows, a list of files in a directory) for a file. That doesn't make sense. It works, somewhat, because you get the exception for trying to access a directory "somefile.txt".

You should be able to do it the right way (tm) by looking at the output of the ListDirectory response of the parent:

Do a ListDirectory ftp://yourserver/ and check if

  • your file
  • your directory

is listed.

Benjamin Podszun
then if I need to go for some dir then why I don't find any exception in the code.like ftp://someip.com/folder/folder/this dosen't return any exception if there is no folder in folder directory.
marshalprince
ok. that is a way but it creates a extra overhead in the code. Why the response dosen't comeout to be null or thrown a exception if it doen't there.
marshalprince
A: 
request = (FtpWebRequest)WebRequest.Create("ftp://" + webrequestUrl); //no file name
request.Credentials = new NetworkCredential(username, password);
myFtpRequest.Method = WebRequestMethods.Ftp.ListDirectory;

And check if your file/dir is listed.

You need to interrogate the response, it should contain a list of possible files and directorys.

You should not be using a catch to handle program flow. MSDN example

runrunraygun
A: 

I use:

private bool CreateFTPDirectory(string directory)
{

    try
    {
        //create the directory
        FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpURI+"/"+directory));
        requestDir.Method = WebRequestMethods.Ftp.MakeDirectory;
        requestDir.UsePassive = true;
        requestDir.UseBinary = true;
        requestDir.KeepAlive = false;
        //requestDir.UseDefaultCredentials = true;
        requestDir.Credentials = new NetworkCredential(UserId, Password);
        requestDir.Proxy = WebRequest.DefaultWebProxy;
        requestDir.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

        FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse();
        Stream ftpStream = response.GetResponseStream();

        ftpStream.Close();
        response.Close();

        return true;
    }
    catch (WebException ex)
    {
        FtpWebResponse response = (FtpWebResponse)ex.Response;
        if ((response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) || (((int)response.StatusCode)==521))
        {
            response.Close();
            return true;
        }
        else
        {
            response.Close();
            return false;
        }
    }
}

This has the side effect of creating the directory as well. If it already exists you get a 521 result returned which isn't defined in the .NET Enum.

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.

Tony Lambert