Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
7.2k views
in .NET FTP by (160 points)
Hey All,

I am using VB.NET 2008 w/ the latest free version of this component.

For the sake of this post I have declared this component as objFTPConnection

There does not seem to be a good way to check if a directory exists.

The objFTPConnection.Exists method seems to only work for files.

The objFTPConnection.ChangeWorkingDirectory states it returns type boolean -- yet this does not seem to work. I can not compare this to a true/false clause.

What is the best way to check if a directory exists?

7 Answers

0 votes
by (162k points)
Try ChangeWorkingDirectory - if it fails with an exception, it does not exist.

We will be adding DirectoryExists and FileExists methods very soon - DirectoryExists will do the above.
0 votes
by (160 points)
Thank you for the prompt reply.

Any idea on time frame for those commands?

I guess I will do a try/catch for the change directory.

Thanks!
0 votes
by (162k points)
Planned for the next release but that won't be for a few weeks. We'll implement it in exactly the same way.
0 votes
by (240 points)
Sorry for resurrecting an old thread but I'm looking for this feature too.
The latest edt free version has the FTPConnection.Exists function that checks if a file exists or not. I tried to use it to check for the existence of a directory and I had mixed results.
It works for zFtpServer but not for FileZilla server. Both are hosted on a Windows machine.
Any idea how I can have a foolproof but straightforward checking for a directory's existence? I am quite hesitant on going the GetFileInfos route as I have quite a number of files and subdirectories.
0 votes
by (51.6k points)
The best generic approach is to try to change into the directory. So something like this:
public bool DirectoryExists(FTPConnection ftpConnection, string remoteDirectory)
{
   string initDir = ftpConnection.ServerDirectory;
   try
   {
      ftpConnection.ChangeWorkingDirectory(remoteDirectory);
   }
   catch (Exception)
   {
      return false;
   }
   ftpConnection.ChangeWorkingDirectory(initDir);
   return true;
}


This method is already in our code-base. I'm not sure if it's made it into the current release of whichever product you're using. If not then it will be in the next version.

- Hans (EnterpriseDT)
0 votes
by (240 points)
Thanks for the input!
The only issue I have with it, although a very minor one, is the use of try-catch statement as it comes with a small overhead.
Also, does ChangeWorkingDirectory work even if the target directory is several levels up or down?
For instance I am on /FolderA and the target folder is /FolderA/FolderA1/FolderA1a.
0 votes
by (51.6k points)
I expect that the time spent in the try-catch is negligible when compared with the time spent communicating with the remote host while executing the cwd.

The exact behaviour of cwd depends on the server. I've never come across a server that doesn't support changing multiple directory levels in one operation. Once or twice I've seen reports of a server that doesn't support absolute directories, though I think they were particularly ancient ones. So the method I listed won't work universally, but it should work on the vast majority of servers.

- Hans (EnterpriseDT)

Categories

...