0

I want to fetch the files from folder through FTP using c#, I have folder name called MyFolder, inside of this folder i have multiple folder, i need to fetch each file from all this folders which is inside the my MyFolder.Below code which i am getting all directories,Now i need to get each file.

 Eg:httpdocs/Myfolder/newfolder/newfile.txt 
                               /newfile1.txt  
                               /newfile2.txt 
    httpdocs/Myfolder/newfolder1/newfile.txt 
    httpdocs/Myfolder/newfolder2/newfile.txt   


        FtpWebRequest ftpRequest =(FtpWebRequest)WebRequest.Create("ftp://www.xxxxxxx.com/httpdocs/MyFolder");
        ftpRequest.Credentials = new NetworkCredential("xxxxx", "xxxxxx");
        ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
        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();
        }
        streamReader.Close();
    }

1 Answers1

-1

Have you had a look at the MSDN Documentation? https://msdn.microsoft.com/de-de/library/ms229711(v=vs.110).aspx

public static void Main ()
    {
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
        request.Method = WebRequestMethods.Ftp.DownloadFile;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        Console.WriteLine(reader.ReadToEnd());

        Console.WriteLine("Download Complete, status {0}", response.StatusDescription);

        reader.Close();
        response.Close();  
    }

Edit: Already on StackOverflow Have a look here.