0
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUrl);
        request.Method = WebRequestMethods.Ftp.ListDirectory;
        var response = (FtpWebResponse)request.GetResponse();
        Stream rStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(rStream);
        string fileNames = reader.ReadToEnd();
        List<string> ls = fileNames.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
        for (int i = 0; i < ls.Count; i++)
            Console.WriteLine(ls[i]);

This method only lists files present in the specified serverUrl directory . I want to list all the files of the FTP server. Can anyone suggest me anything?

shashi9915
  • 13
  • 2
  • You are looking for the `dir -R` ftp command (capital R is important). No clue what it is in .net but that's the one you want. It list all files in the directory and all subdirectory with their files as well – Franck Nov 24 '20 at 14:37
  • How do you feel about recursive algorithms? The `Ftp.ListDirectory` command only lists the contents of one folder. It also lists sub-directories. You could use recursion to list the files in sub-directories too. However, it could get messy if you don't think of a nice way of showing the paths (including sub-directories). – tgolisch Nov 24 '20 at 15:20

1 Answers1

0

I have written the related code in the msdn forum like: download each file in folder.

You can use the FtplistFile method to get a list of all files in the ftp server.

Here is a code example I modified.

private void button1_Click(object sender, EventArgs e)
    {
        string uri = "ftp://xxx.x.xx.x/";
        string username = "USERNAME";
        string password = "PASSWORD";
        ListFtpDirectory(uri, new NetworkCredential(username, password));

    }

    static void ListFtpDirectory(string url, NetworkCredential credentials)
    {
        WebRequest listRequest = WebRequest.Create(url);
        listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
        listRequest.Credentials = credentials;

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

        using (WebResponse listResponse = listRequest.GetResponse())
        using (Stream listStream = listResponse.GetResponseStream())
        using (StreamReader listReader = new StreamReader(listStream))
        {
            while (!listReader.EndOfStream)
            {
                string line = listReader.ReadLine();
                lines.Add(line);
            }
        }

        foreach (string line in lines)
        {
            string[] tokens =
                line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
            string name = tokens[3];
            string permissions = tokens[2];

            if (permissions == "<DIR>")
            {
                Console.WriteLine($"Directory {name}");
                string fileUrl = url + name;
                ListFtpDirectory(fileUrl + "/", credentials);
            }
            else
            {
                Console.WriteLine(url+name);
                Console.WriteLine($"File {name}");
            }
        }
    }

Get file name:

enter image description here

Get file path in ftp:

enter image description here

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27
  • As I said. This only lists the files and directories of a specific ftp directory url. I want to list all the files by just using root url – shashi9915 Nov 25 '20 at 04:06
  • @shashi9915, I have updated my code, you can check if it works for you. If you don't want to show the directory name, you can delete the code Console.WriteLine($"Directory {name}"); – Jack J Jun Nov 25 '20 at 06:44
  • Thanks. I found another way to list all the files but a lengthy process, takes much time. Your answer is way better. Is there any way to display the path of the file??? – shashi9915 Nov 25 '20 at 12:04
  • @shashi9915, you can use the code Console.WriteLine(url+name); to get the file path in ftp, I have edited it. – Jack J Jun Nov 26 '20 at 03:26
  • I tried to populate the treeview with these directories and files. The problem arises with recursive function in the above code. Any suggestions? – shashi9915 Nov 26 '20 at 10:30
  • @shashi9915, I suggest that you can ask a new question about it. – Jack J Jun Nov 30 '20 at 09:11