Download / Upload Binary Files on FTP Server Using C#

Here I am presenting code to download and upload files to FTP Servers.

We will use the FtpWebRequest and FtpWebResponse classes. The reason to use these classes instead of using WebClient classes is that if we use FtpWebRequest/Response classes to upload and download files then we have more control of what is done inside. The code I am showing here is easy to understand; most of the prarameters I have specified are commented out so you can understand easier. First you will need two namespaces to work with FTP; they are:

using System.Net;
using System.IO;

And here are the two functions I have made to download and Upload files.

Uploading File

    /// Upload File to Specified FTP Url with username and password and Upload Directory
            if need to upload in sub folders ///
    ///Base FtpUrl of FTP Server
    ///Local Filename to Upload
    ///Username of FTP Server
    ///Password of FTP Server
    ///[Optional]Specify sub Folder if any
    /// Status String from Server
    public static string UploadFile(string FtpUrl, string fileName, string userName, string password,string
    UploadDirectory="")
    {
        string PureFileName = new FileInfo(fileName).Name;
        String uploadUrl = String.Format("{0}{1}/{2}", FtpUrl,UploadDirectory,PureFileName);
        FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(uploadUrl);
        req.Proxy = null;
        req.Method = WebRequestMethods.Ftp.UploadFile;
        req.Credentials = new NetworkCredential(userName,password);
        req.UseBinary = true;
        req.UsePassive = true;
        byte[] data = File.ReadAllBytes(fileName);
        req.ContentLength = data.Length;
        Stream stream = req.GetRequestStream();
        stream.Write(data, 0, data.Length);
        stream.Close();
        FtpWebResponse res = (FtpWebResponse)req.GetResponse();
        return res.StatusDescription;
    }

Downloading File

    /// Download File From FTP Server ///
    ///Base url of FTP Server
    ///if file is in root then write FileName Only if is in use like "subdir1/subdir2/filename.ext"
    ///Username of FTP Server
    ///Password of FTP Server
    ///Folderpath where you want to Download the File
    /// Status String from Server
    public static string DownloadFile(string FtpUrl,string FileNameToDownload,
                        string
userName, string password,string tempDirPath)
    {
        string ResponseDescription = "";
        string PureFileName = new FileInfo(FileNameToDownload).Name;
        string DownloadedFilePath  =  tempDirPath+"/"+PureFileName;
        string downloadUrl = String.Format("{0}/{1}", FtpUrl, FileNameToDownload);
        FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(downloadUrl);
        req.Method = WebRequestMethods.Ftp.DownloadFile;
        req.Credentials = new NetworkCredential(userName, password);
        req.UseBinary = true;
        req.Proxy = null;
        try
        {
            FtpWebResponse response = (FtpWebResponse)req.GetResponse();
            Stream stream = response.GetResponseStream();
            byte[] buffer = new byte[2048];
            FileStream fs = new FileStream(DownloadedFilePath, FileMode.Create);
            int ReadCount = stream.Read(buffer, 0, buffer.Length);
            while (ReadCount > 0)
            {
                fs.Write(buffer, 0, ReadCount);
                ReadCount = stream.Read(buffer, 0, buffer.Length);
            }
            ResponseDescription = response.StatusDescription;
            fs.Close();
            stream.Close();
        }
        catch(Exception e)
        {
            Console.WriteLine(e.Message);
        }
        return ResponseDescription;
    }

Up Next
    Ebook Download
    View all
    Learn
    View all