List Folders and Subfolders of an FTP with C #

2

asked by anonymous 03.02.2016 / 21:03

2 answers

2

I use Regex, along with a detailed FTP return. Here is 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 output is:

atomic-doc é um diretorio
bin -> usr/bin é um arquivo
... 
    
08.02.2016 / 05:13
1

On the official MSDN blog you have an article with an example of how to list folder contents 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();
    
05.01.2017 / 09:32