Two nights ago I created an app that lets me just right-click a file in Windows Explorer and it uploads the file to my FTP site in a temp directory so I can quickly and easily share files with friends.  Just click it’s uploaded, and click the resulting URL is copied into the clipboard to be pasted into an e-mail, Word, blog post, etc.

To do this, I needed to upload a file to an FTP site.  There is a FtpWebRequest class in System.Net, but it was cryptic to use for just a simple file upload.  Our MSDN documents could definitely use more sample code!  Hopefully the new MSDN Wiki will help.  But after much searching, I came across BytesRoad.NetSuit, a free .NET library that was real easy to use!  It does more than just FTP, but here’s my code showing how easy it is to upload a file.

using System.IO;

using BytesRoad.Net.Ftp;

 

namespace CoadTools.Ftp

{

  public class FtpToools

  {

    public static void UploadFile(int Timeout, string FtpServer,

      string Username, string Password, string RemotePath,

      string LocalFile)

    {

      // get instance of the FtpClient

      FtpClient client = new FtpClient();

 

      // use passive mode

      client.PassiveMode = true;

 

      // connect to the specified FTP server

      client.Connect(Timeout, FtpServer, 21);

      client.Login(Timeout, Username, Password);

 

      // build the target file path

      string target = Path.Combine(RemotePath,

        Path.GetFileName(LocalFile)).Replace("\\", "/");

 

      // synchronously upload the file

      client.PutFile(Timeout, target, LocalFile);

    }

  }

}