0

I need to find all files and all subfolders in a directory. I am omitting the FtpWebRequest. Here is what I have written so far:

 private string[] fileList () {

    StringBuilder result = new StringBuilder();
    WebResponse response = null;
    FtpWebRequest reqFtp = null;

    //makes request to ftp server

    StreamReader reader = new StreamReader(response.GetResponseStream());
    string line = reader.ReadLine();

    //reads everything in directory but does not open the subfolders    
    while (line != null)
    {
        result.Append(line);
        result.Append("\n");
        line = reader.ReadLine();
    }

    result.Remove(result.ToString().LastIndexOf('\n');
    return result.ToString().Split('\n');
}

This shows me all files and folders within the specified directory. However, my question is: How do I read the files that are in each subfolder within this directory?

Is there a way of detecting that I've reached a folder so that I can save the path as an index in string[] perhaps, and keep reading until there are no more folders?

My intention is to overwrite each file on my local machine with the files found being read from the Ftp Server.

avidprogrammer
  • 57
  • 1
  • 11
  • Recursively list files in a directory: http://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a-directory-in-c – tnw Apr 24 '13 at 18:12

1 Answers1

1

After you have a list of files and/or directories you will have to exectue a new ftp request to get items in that list. For example if, I request the contents of a dir and get a list of files you will then need to loop over that list making a request for each file. Then inside the loop overwrite your local copy. You'll basically handle it like you would local files only use an ftp request and read the stream where you would have normally opened the file and read the stream.

This question shows how to recursively get all files in current dir and sub dirs. How to recursively list all the files in a directory in C#?

If you're on the same domain you can use UNC paths with the normal dir/file objects and forgo the ftp request entirely.

Community
  • 1
  • 1
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • For each file that is different (in bytes), I need to make a new request? That does not seem right to me. – avidprogrammer Apr 24 '13 at 18:22
  • @avidprogrammer your stream is the contents of the dir, not the contents of the files. If it were, your loop would be reading the file contents. So yes, you'll have to open a new stream for each file. When you read files on your local harddrive can you wrap them all up into one stream? Same goes here. – evanmcdonnal Apr 24 '13 at 18:26