List Folders and Subfolders of an FTP with C#

Asked

Viewed 1,387 times

2

Would anyone know if it is possible, if yes, how to list all folders and subfolders of a FTP?

Okay, here’s the idea. I’m listing all the Files and Directories in this FTP, but different from DirectoryInfo().GetFiles(), via FTP you don’t have that option.

Someone’s done something like this?

2 answers

2

I use Regex, along with detailed ftp return. Follow the code:

static string regex =
    @"^" +                          //# Start of line
    @"(?<dir>[\-ld])" +             //# File size          
    @"(?<permission>[\-rwx]{9})" +  //# Whitespace          \n
    @"\s+" +                        //# Whitespace          \n
    @"(?<filecode>\d+)" +
    @"\s+" +                        //# Whitespace          \n
    @"(?<owner>\w+)" +
    @"\s+" +                        //# Whitespace          \n
    @"(?<group>\w+)" +
    @"\s+" +                        //# Whitespace          \n
    @"(?<size>\d+)" +
    @"\s+" +                        //# Whitespace          \n
    @"(?<month>\w{3})" +            //# Month (3 letters)   \n
    @"\s+" +                        //# Whitespace          \n
    @"(?<day>\d{1,2})" +            //# Day (1 or 2 digits) \n
    @"\s+" +                        //# Whitespace          \n
    @"(?<timeyear>[\d:]{4,5})" +    //# Time or year        \n
    @"\s+" +                        //# Whitespace          \n
    @"(?<filename>(.*))" +          //# Filename            \n
    @"$";                           //# End of line

static void Main(string[] args)
{
    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://ftp.isi.edu/");
    ftpRequest.Credentials = new NetworkCredential("anonymous", "[email protected]");
    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
    StreamReader streamReader = new StreamReader(response.GetResponseStream());

    List<string> directories = new List<string>();

    string line = streamReader.ReadLine();
    while (!string.IsNullOrEmpty(line))
    {
        directories.Add(line);
        line = streamReader.ReadLine();
        if (line != null)
        {
            var split = new Regex(regex).Match(line);//separo no formato regex
            string dir = split.Groups["dir"].ToString();//vejo a parte dir
            string filename = split.Groups["filename"].ToString(); //vejo a parte de arquivo
            bool isDirectory = !string.IsNullOrWhiteSpace(dir) && dir.Equals("d", StringComparison.OrdinalIgnoreCase);//verifico se e uma pasta atravez do dir

             if(isDirectory)//é pasta
               Console.WriteLine(filename + " é um diretorio");
             else//nao e pasta
               Console.WriteLine(filename + " é um arquivo");
        }
    }

    streamReader.Close();

    Console.ReadKey();
}

My way out is:

atomic-doc é um diretorio
bin -> usr/bin é um arquivo
... 

1

In the official blog of MSDN has an article with an example of how to list contents of the folder with FTP.

// Get the object used to communicate with the server.
var request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/");
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("anonymous","[email protected]");

var response = (FtpWebResponse)request.GetResponse();

var responseStream = response.GetResponseStream();
var reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());

Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);

reader.Close();
response.Close();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.