0
public IEnumerable<FTPListDetail> GetDirectoryListing(string rootUri)
        {
            var CurrentRemoteDirectory = rootUri;
            var result = new StringBuilder();
            var request = GetWebRequest(WebRequestMethods.Ftp.ListDirectoryDetails, CurrentRemoteDirectory);
            using (var response = request.GetResponse())
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    string line = reader.ReadLine();
                    while (line != null)
                    {
                        result.Append(line);
                        result.Append("\n");
                        line = reader.ReadLine();
                    }
                    if (string.IsNullOrEmpty(result.ToString()))
                    {
                        return new List<FTPListDetail>();
                    }
                    result.Remove(result.ToString().LastIndexOf("\n"), 1);
                    var results = result.ToString().Split('\n');
                    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

                    var myresult = new List<FTPListDetail>();
                    foreach (var parsed in results)
                    {
                        var split = new Regex(regex)
                            .Match(parsed);
                        var dir = split.Groups["dir"].ToString();
                        var permission = split.Groups["permission"].ToString();
                        var filecode = split.Groups["filecode"].ToString();
                        var owner = split.Groups["owner"].ToString();
                        var group = split.Groups["group"].ToString();
                        var filename = split.Groups["filename"].ToString();
                        var size = split.Groups["size"].Length;
                        myresult.Add(new FTPListDetail()
                        {
                            Dir = dir,
                            Filecode = filecode,
                            Group = group,
                            FullPath = CurrentRemoteDirectory + "/" + filename,
                            Name = filename,
                            Owner = owner,
                            Permission = permission,
                        });
                    };
                    return myresult;
                }
            }
        }

I'm getting list of files and directories from my ftp server.

I'm calling this method from another method recursive so first time in CurrentRemoteDirectory im getting the root directory then a sub directory from the root and another sub directory and so on...

My question is how can i identify and use the first time directory i know it's the root but how the program will know its the root ? Maybe using a Tag somehow ?

Louie Bacaj
  • 1,417
  • 13
  • 18
Daniel Hamutel
  • 643
  • 1
  • 13
  • 30
  • recursion usually means: Start here, then keep going until you reach the end. why do you need to know for each iteration what the root directory is? – MaxOvrdrv Feb 18 '15 at 17:55
  • Would this answer your question? http://stackoverflow.com/a/3299039/3066687 – aRBee Feb 18 '15 at 18:11
  • "Recursively" would typically mean that the method calls itself. But in your code example, there is no place in the method where it appears to call itself. It's also not at all clear what you mean by "root" (simply the string passed to the first call of the method? something relative to the actual file system? something else?). But unless you show an actual recursive method, I don't see how anyone can answer a question about a recursive method. – Peter Duniho Feb 18 '15 at 18:15

1 Answers1

0

Create another function or do it in place where you test your rootUri if it is the root directory for example like so:

    public static bool IsRoot(string directory)
    {
        //Set the current directory - this can throw an exception either catch this here or outside.
        Directory.SetCurrentDirectory(directory);

        return Directory.GetDirectoryRoot(directory).Equals(Directory.GetCurrentDirectory());
    }
Louie Bacaj
  • 1,417
  • 13
  • 18