# Friday, 29 June 2018

The .NET framework provides the FtpWebRequest class to assist you in managing FTP commands.

A common use of this class is to upload a file to an FTP site.

To do this in C#, we create an FtpWebRequest object (the WebRequest object has a static factory method to do this) on the object, set our intended method, and specify the FTP site's login credentials, as shown below.

string ftpUrl = @"ftp://blah.ftp.blah.com";
ftpUrl = ConfigurationManager.AppSettings["ftpUri"];
string userName = ConfigurationManager.AppSettings["userName"];
string password = ConfigurationManager.AppSettings["password"];

string ftpDestinationFolder = "site/wwwroot/content/Giard";
string localFolderSource = @"C:\Test\_source";
string fileName = "testfile.txt";

string sourcePath = @"C:\Test\_source\testfile.txt";
string destinationPath = @"ftp://blah.ftp.blah.com/folder1/testfile.txt";

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(destinationPath);
request.Method = WebRequestMethods.Ftp.UploadFile;
  

A common mistake is to set only the site and folder name (and skip the filename) in the destinationPath.

To send a file via FTP, we need to read the file from disk and convert it into a Stream.

byte[] fileContents;
using (StreamReader sourceStream = new StreamReader(sourcePath))
{
	fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
}
  

We then write this StreamReader to the FTP request object

request.ContentLength = fileContents.Length;
using (Stream requestStream = request.GetRequestStream())
{
	requestStream.Write(fileContents, 0, fileContents.Length);
}
  

Finally, we retrieve the FTP response.

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
	var responseText = response.StatusDescription;
	Console.WriteLine("Completed uploading file {0}!", fileName);
	Console.WriteLine("Status={0}", responseText);
}
  

The full code of my console app is here:

using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;

namespace DemoFtp
{
    class Program
    {
        static void Main(string[] args)
        {
            string ftpUrl = @"ftp://blah.ftp.blah.com";
            // Note: I stored some values in app.config
            // I had to set a reference to System.Configuration to read these values
            ftpUrl = ConfigurationManager.AppSettings["ftpUri"];
            string userName = ConfigurationManager.AppSettings["userName"];
            string password = ConfigurationManager.AppSettings["password"];

            string ftpDestinationFolder = "site/wwwroot/content/Giard";
            string localFolderSource = @"C:\Test\_source";
            string fileName = "testfile.txt";

            string sourcePath = Path.Combine(localFolderSource, fileName);
            string destinationPath = Path.Combine(ftpUrl, ftpDestinationFolder, fileName);

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(destinationPath);
            request.Method = WebRequestMethods.Ftp.UploadFile;

            request.Credentials = new NetworkCredential(userName, password);

            byte[] fileContents;
            using (StreamReader sourceStream = new StreamReader(sourcePath))
            {
                fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            }

            request.ContentLength = fileContents.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(fileContents, 0, fileContents.Length);
            }

            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                var responseText = response.StatusDescription;
                Console.WriteLine("Completed uploading file {0}!", fileName);
                Console.WriteLine("Status={0}", responseText);
            }

            Console.ReadLine();
        }
    }
}
  

You can find this console app in my github repository at https://github.com/DavidGiard/Demo-FTP-CSharp

.Net | C# | Web
Comments are closed.