Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top