1

For a ftp path say ftp://ftp.something.com/ I am able to list all directories with this code :

WebRequest req = WebRequest.Create(url) as WebRequest;
        req.Method = WebRequestMethods.Ftp.ListDirectory;

//code to get response from ftp site and list all files and directories path in a list name name_list.

Now foreach path from a list name_list, if path is a directory then I add that path in a list name sub_list else if it is path of some file(.txt, .pdf, .rar, .html, .tw and many more extensions) then add that path in another list name final_list. So far what I am able to do is :

foreach(string url in name_list)
{
 if (Regex.IsMatch(url, ".*?" + @"(\.[A-Za-z]{2,4}$)"))
         //add to sub_list
else
        //add to final_list
}

But this is not a reliable and robust way to achieve my goal. Is there any other best way to this.

Manish
  • 517
  • 1
  • 3
  • 19
  • Sicne the `ListDirectory` is actually an "FTP NLIST" command, you could take a look at [this similar SO question](http://stackoverflow.com/questions/584865/determine-if-a-listing-is-a-directory-or-file-in-python-over-ftp). – Uwe Keim Jul 19 '12 at 11:11

2 Answers2

0

I would use the System.IO.Path class to determine if it is a path or file. Specifically: http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx

  • 2
    This is FTP-related, I doubt that you can use the _local_ file system functions for that. – Uwe Keim Jul 19 '12 at 11:08
  • The questioner is looking for a method to determine if the string url matches a dir or file pattern. For this the Path class would work regardless of the url. – Oliver Clancy Jul 19 '12 at 18:33
0

You can look at file permissions to see if it is a directory or not. Refer to the following example:

http://www.copyandwaste.com/posts/view/parsing-webrequestmethodsftplistdirectorydetails-and-listdirectory/

ZanderAdam
  • 434
  • 2
  • 13