Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
3.7k views
in .NET FTP by (400 points)
Hi Support,

We are using EDT FTP free component. Over here we have a requirement in which we need to create a directory on the FTP server at runtime, but before that we need to check whether that directory exists in FTP server or not. We are using FTPConnection class in our project. Can you please let us know if there is any way to check the directory's existence in FTP server? Please advise us on this item. Please let us know ASAP as it is very urgent for us to deliver the project.

Thanks for your kind attention and support.

Many Thanks
Kris

5 Answers

0 votes
by (51.6k points)
Hi Kris

We will be adding an Exists(string) method to FTPConnection soon, but until then you can use either GetLastWriteTime(string) or GetSize(string). These methods will throw an exception if the file doesn't exist. Alternatively, you can simply use the GetFiles() method to get a listing of all files and then iterate through that listing to see if your file is there.

- Hans (EDT)
0 votes
by (240 points)
try {
  //Try to change to the directory....
  ftpClient.ChDir(ConfigurationSettings.AppSettings["ftpServerDir"] + id.ToString() + "//");
  DisplayStatus("Switching to Existing Directory: " + ConfigurationSettings.AppSettings["ftpServerDir"] + id.ToString() + "...");
} catch (Exception ex) {
  //It doesn't exist, create it...
  DisplayStatus("Creating Directory: " + ConfigurationSettings.AppSettings["ftpServerDir"] + id.ToString() + "...");
  ftpClient.MkDir(ConfigurationSettings.AppSettings["ftpServerDir"] + id.ToString() + "//");
            
  DisplayStatus("Directory Created, Switching to it...");
  ftpClient.ChDir(ConfigurationSettings.AppSettings["ftpServerDir"] + id.ToString() + "//");
}
0 votes
by (51.6k points)
The Exists method has now been added and is in the current version that is available from http://www.enterprisedt.com/products/edtftpnet/overview.html

- Hans (EDT)
0 votes
by (140 points)
Does this Exists method support directories. My application is changing to a directory and then needs to determine if a new subdirectory exists or not so i am calling the following code:

ftp.ChDir(rootDir);
if (!ftp.Exists("subdir"))
{
     ftp.MkDir("subdir");
}

When I call exists, I am getting a false returned so I try to create the directory which really does already exists and I get an exception.

Thanks for any advice you have on this.

Brian
0 votes
by (162k points)
Good question. No, it is for files only.

We will make a note to extend it to directories though - that would obviously be useful.

In the interim, the best way to see if a directory exists is to use GetFileInfos() or to try to change into the directory.

Categories

...