C# How to download files from FTP Server

Posted by user3696888 on Stack Overflow See other posts from Stack Overflow or by user3696888
Published on 2014-06-01T14:03:26Z Indexed on 2014/06/01 15:26 UTC
Read the original article Hit count: 173

Filed under:
|

I'm trying to download a file (all kinds of files, exe dll txt etc.). And when I try to run it an error comes up on:

using (FileStream ws = new FileStream(destination, FileMode.Create))

This is the error message:

Access to the path 'C:\Riot Games\League of Legends\RADS\solutions  
\lol_game_client_sln\releases\0.0.1.41\deploy'(which is my destination, where I want 
to save it) is denied.

Here is my code

void download(string url, string destination)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential("user", "password");
        request.UseBinary = true;

        using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
        {
            using (Stream rs = response.GetResponseStream())
            {
                using (FileStream ws = new FileStream(destination, FileMode.Create))
                {
                    byte[] buffer = new byte[2048];
                    int bytesRead = rs.Read(buffer, 0, buffer.Length);

                    while (bytesRead > 0)
                    {
                        ws.Write(buffer, 0, bytesRead);
                        bytesRead = rs.Read(buffer, 0, buffer.Length);
                    }
                }
            }
        }

© Stack Overflow or respective owner

Related posts about c#

Related posts about ftp