SoFunction
Updated on 2025-03-01

Share the client C# class used to operate FTP

This is a client C# class for operating FTP. The class has encapsulated various commonly used FTP operation methods. The call is very simple. You don’t need to care about the details of ftp connection and operation. Just call the relevant methods in this class.

using System;
using ;
using ;
using ;
using ;
using ;
 
namespace 
{
  public class FTPClient
  {
    public static object obj = new object();
 
    #region constructor    /// <summary>
    /// Default constructor    /// </summary>
    public FTPClient()
    {
      strRemoteHost = "";
      strRemotePath = "";
      strRemoteUser = "";
      strRemotePass = "";
      strRemotePort = 21;
      bConnected = false;
    }
 
    /// <summary>
    /// Constructor    /// </summary>
    public FTPClient(string remoteHost, string remotePath, string remoteUser, string remotePass, int remotePort)
    {
      strRemoteHost = remoteHost;
      strRemotePath = remotePath;
      strRemoteUser = remoteUser;
      strRemotePass = remotePass;
      strRemotePort = remotePort;
      Connect();
    }
    #endregion
 
    #region field    private int strRemotePort;
    private Boolean bConnected;
    private string strRemoteHost;
    private string strRemotePass;
    private string strRemoteUser;
    private string strRemotePath;
 
    /// <summary>
    /// The reply information returned by the server (including the answer code)    /// </summary>
    private string strMsg;
    /// <summary>
    /// The reply information returned by the server (including the answer code)    /// </summary>
    private string strReply;
    /// <summary>
    /// The reply code returned by the server    /// </summary>
    private int iReplyCode;
    /// <summary>
    /// socket for controlling connection    /// </summary>
    private Socket socketControl;
    /// <summary>
    /// Transmission mode    /// </summary>
    private TransferType trType;
    /// <summary>
    /// Buffer for receiving and sending data    /// </summary>
    private static int BLOCK_SIZE = 512;
    /// <summary>
    /// Coding method    /// </summary>
    Encoding ASCII = ;
    /// <summary>
    /// Byte array    /// </summary>
    Byte[] buffer = new Byte[BLOCK_SIZE];
    #endregion
 
    #region properties    /// <summary>
    /// FTP server IP address    /// </summary>
    public string RemoteHost
    {
      get
      {
        return strRemoteHost;
      }
      set
      {
        strRemoteHost = value;
      }
    }
 
    /// <summary>
    /// FTP server port    /// </summary>
    public int RemotePort
    {
      get
      {
        return strRemotePort;
      }
      set
      {
        strRemotePort = value;
      }
    }
 
    /// <summary>
    /// Current server directory    /// </summary>
    public string RemotePath
    {
      get
      {
        return strRemotePath;
      }
      set
      {
        strRemotePath = value;
      }
    }
 
    /// <summary>
    /// Log in to the user account    /// </summary>
    public string RemoteUser
    {
      set
      {
        strRemoteUser = value;
      }
    }
 
    /// <summary>
    /// User login password    /// </summary>
    public string RemotePass
    {
      set
      {
        strRemotePass = value;
      }
    }
 
    /// <summary>
    /// Whether to log in    /// </summary>
    public bool Connected
    {
      get
      {
        return bConnected;
      }
    }
    #endregion
 
    #region link    /// <summary>
    /// Create a connection    /// </summary>
    public void Connect()
    {
      lock (obj)
      {
        socketControl = new Socket(, , );
        IPEndPoint ep = new IPEndPoint((RemoteHost), strRemotePort);
        try
        {
          (ep);
        }
        catch (Exception)
        {
          throw new IOException("Cannot connect to ftp server");
        }
      }
      ReadReply();
      if (iReplyCode != 220)
      {
        DisConnect();
        throw new IOException((4));
      }
      SendCommand("USER " + strRemoteUser);
      if (!(iReplyCode == 331 || iReplyCode == 230))
      {
        CloseSocketConnect();
        throw new IOException((4));
      }
      if (iReplyCode != 230)
      {
        SendCommand("PASS " + strRemotePass);
        if (!(iReplyCode == 230 || iReplyCode == 202))
        {
          CloseSocketConnect();
          throw new IOException((4));
        }
      }
      bConnected = true;
      ChDir(strRemotePath);
    }
 
    /// <summary>
    /// Close the connection    /// </summary>
    public void DisConnect()
    {
      if (socketControl != null)
      {
        SendCommand("QUIT");
      }
      CloseSocketConnect();
    }
    #endregion
 
    #region Transmission Mode    /// <summary>
    /// Transmission mode: binary type, ASCII type    /// </summary>
    public enum TransferType { Binary, ASCII };
 
    /// <summary>
    /// Set the transmission mode    /// </summary>
    /// <param name="ttType">Transfer mode</param>    public void SetTransferType(TransferType ttType)
    {
      if (ttType == )
      {
        SendCommand("TYPE I");//binary type transmission      }
      else
      {
        SendCommand("TYPE A");//ASCII type transmission      }
      if (iReplyCode != 200)
      {
        throw new IOException((4));
      }
      else
      {
        trType = ttType;
      }
    }
 
    /// &lt;summary&gt;
    /// Obtain transmission mode    /// &lt;/summary&gt;
    /// <returns>Transfer Mode</returns>    public TransferType GetTransferType()
    {
      return trType;
    }
    #endregion
 
    #region file operation    /// &lt;summary&gt;
    /// Get a file list    /// &lt;/summary&gt;
    /// <param name="strMask">Match string for filename</param>    public string[] Dir(string strMask)
    {
      if (!bConnected)
      {
        Connect();
      }
      Socket socketData = CreateDataSocket();
      SendCommand("NLST " + strMask);
      if (!(iReplyCode == 150 || iReplyCode == 125 || iReplyCode == 226))
      {
        throw new IOException((4));
      }
      strMsg = "";
      (2000);
      while (true)
      {
        int iBytes = (buffer, , 0);
        strMsg += (buffer, 0, iBytes);
        if (iBytes &lt; )
        {
          break;
        }
      }
      char[] seperator = { '\n' };
      string[] strsFileList = (seperator);
      (); //There will also be a return code when the data socket is closed      if (iReplyCode != 226)
      {
        ReadReply();
        if (iReplyCode != 226)
        {
 
          throw new IOException((4));
        }
      }
      return strsFileList;
    }
 
    public void newPutByGuid(string strFileName, string strGuid)
    {
      if (!bConnected)
      {
        Connect();
      }
      string str = (0, ("\\"));
      string strTypeName = (("."));
      strGuid = str + "\\" + strGuid;
      Socket socketData = CreateDataSocket();
      SendCommand("STOR " + (strGuid));
      if (!(iReplyCode == 125 || iReplyCode == 150))
      {
        throw new IOException((4));
      }
      FileStream input = new FileStream(strGuid, );
      ();
      int iBytes = 0;
      while ((iBytes = (buffer, 0, )) &gt; 0)
      {
        (buffer, iBytes, 0);
      }
      ();
      if ()
      {
        ();
      }
      if (!(iReplyCode == 226 || iReplyCode == 250))
      {
        ReadReply();
        if (!(iReplyCode == 226 || iReplyCode == 250))
        {
          throw new IOException((4));
        }
      }
    }
 
    /// &lt;summary&gt;
    /// Get file size    /// &lt;/summary&gt;
    /// <param name="strFileName">File name</param>    /// <returns>File size</returns>    public long GetFileSize(string strFileName)
    {
      if (!bConnected)
      {
        Connect();
      }
      SendCommand("SIZE " + (strFileName));
      long lSize = 0;
      if (iReplyCode == 213)
      {
        lSize = ((4));
      }
      else
      {
        throw new IOException((4));
      }
      return lSize;
    }
 
 
    /// &lt;summary&gt;
    /// Get file information    /// &lt;/summary&gt;
    /// <param name="strFileName">File name</param>    /// <returns>File size</returns>    public string GetFileInfo(string strFileName)
    {
      if (!bConnected)
      {
        Connect();
      }
      Socket socketData = CreateDataSocket();
      SendCommand("LIST " + strFileName);
      string strResult = "";
      if (!(iReplyCode == 150 || iReplyCode == 125
        || iReplyCode == 226 || iReplyCode == 250))
      {
        throw new IOException((4));
      }
      byte[] b = new byte[512];
      MemoryStream ms = new MemoryStream();
 
      while (true)
      {
        int iBytes = (b, , 0);
        (b, 0, iBytes);
        if (iBytes &lt;= 0)
        {
 
          break;
        }
      }
      byte[] bt = ();
      strResult = (bt);
      ();
      return strResult;
    }
 
    /// &lt;summary&gt;
    /// delete    /// &lt;/summary&gt;
    /// <param name="strFileName">File name to be deleted</param>    public void Delete(string strFileName)
    {
      if (!bConnected)
      {
        Connect();
      }
      SendCommand("DELE " + strFileName);
      if (iReplyCode != 250)
      {
        throw new IOException((4));
      }
    }
 
    /// &lt;summary&gt;
    /// Rename (if the new file name is the same as the existing file, the existing file will be overwritten)    /// &lt;/summary&gt;
    /// <param name="strOldFileName">Old file name</param>    /// <param name="strNewFileName">New file name</param>    public void Rename(string strOldFileName, string strNewFileName)
    {
      if (!bConnected)
      {
        Connect();
      }
      SendCommand("RNFR " + strOldFileName);
      if (iReplyCode != 350)
      {
        throw new IOException((4));
      }
      // If the new file name is the same as the original file, the original file will be overwritten      SendCommand("RNTO " + strNewFileName);
      if (iReplyCode != 250)
      {
        throw new IOException((4));
      }
    }
    #endregion
 
    #region Upload and download    /// &lt;summary&gt;
    /// Download a batch of files    /// &lt;/summary&gt;
    /// <param name="strFileNameMask">Match string for filename</param>    /// <param name="strFolder">Local directory (cannot end with \)</param>    public void Get(string strFileNameMask, string strFolder)
    {
      if (!bConnected)
      {
        Connect();
      }
      string[] strFiles = Dir(strFileNameMask);
      foreach (string strFile in strFiles)
      {
        if (!(""))// Generally speaking, the last element of strFiles may be an empty string        {
          Get(strFile, strFolder, strFile);
        }
      }
    }
 
    /// &lt;summary&gt;
    /// Download a file    /// &lt;/summary&gt;
    /// <param name="strRemoteFileName">File name to download</param>    /// <param name="strFolder">Local directory (cannot end with \)</param>    /// <param name="strLocalFileName">File name when saved locally</param>    public void Get(string strRemoteFileName, string strFolder, string strLocalFileName)
    {
      Socket socketData = CreateDataSocket();
      try
      {
        if (!bConnected)
        {
          Connect();
        }
        SetTransferType();
        if ((""))
        {
          strLocalFileName = strRemoteFileName;
        }
        SendCommand("RETR " + strRemoteFileName);
        if (!(iReplyCode == 150 || iReplyCode == 125 || iReplyCode == 226 || iReplyCode == 250))
        {
          throw new IOException((4));
        }
        FileStream output = new FileStream(strFolder + "\\" + strLocalFileName, );
        while (true)
        {
          int iBytes = (buffer, , 0);
          (buffer, 0, iBytes);
          if (iBytes &lt;= 0)
          {
            break;
          }
        }
        ();
        if ()
        {
          ();
        }
        if (!(iReplyCode == 226 || iReplyCode == 250))
        {
          ReadReply();
          if (!(iReplyCode == 226 || iReplyCode == 250))
          {
            throw new IOException((4));
          }
        }
      }
      catch
      {
        ();
        socketData = null;
        ();
        bConnected = false;
        socketControl = null;
      }
    }
 
    /// &lt;summary&gt;
    /// Download a file    /// &lt;/summary&gt;
    /// <param name="strRemoteFileName">File name to download</param>    /// <param name="strFolder">Local directory (cannot end with \)</param>    /// <param name="strLocalFileName">File name when saved locally</param>    public void GetNoBinary(string strRemoteFileName, string strFolder, string strLocalFileName)
    {
      if (!bConnected)
      {
        Connect();
      }
 
      if ((""))
      {
        strLocalFileName = strRemoteFileName;
      }
      Socket socketData = CreateDataSocket();
      SendCommand("RETR " + strRemoteFileName);
      if (!(iReplyCode == 150 || iReplyCode == 125 || iReplyCode == 226 || iReplyCode == 250))
      {
        throw new IOException((4));
      }
      FileStream output = new FileStream(strFolder + "\\" + strLocalFileName, );
      while (true)
      {
        int iBytes = (buffer, , 0);
        (buffer, 0, iBytes);
        if (iBytes &lt;= 0)
        {
          break;
        }
      }
      ();
      if ()
      {
        ();
      }
      if (!(iReplyCode == 226 || iReplyCode == 250))
      {
        ReadReply();
        if (!(iReplyCode == 226 || iReplyCode == 250))
        {
          throw new IOException((4));
        }
      }
    }
 
    /// &lt;summary&gt;
    /// Upload a batch of files    /// &lt;/summary&gt;
    /// <param name="strFolder">Local directory (cannot end with \)</param>    /// <param name="strFileNameMask">File name matching characters (can contain * and ?)</param>    public void Put(string strFolder, string strFileNameMask)
    {
      string[] strFiles = (strFolder, strFileNameMask);
      foreach (string strFile in strFiles)
      {
        Put(strFile);
      }
    }
 
    /// &lt;summary&gt;
    /// Upload a file    /// &lt;/summary&gt;
    /// <param name="strFileName">Local file name</param>    public void Put(string strFileName)
    {
      if (!bConnected)
      {
        Connect();
      }
      Socket socketData = CreateDataSocket();
      if ((strFileName) == "")
        SendCommand("STOR " + (strFileName));
      else
        SendCommand("STOR " + (strFileName));
 
      if (!(iReplyCode == 125 || iReplyCode == 150))
      {
        throw new IOException((4));
      }
 
      FileStream input = new FileStream(strFileName, );
      int iBytes = 0;
      while ((iBytes = (buffer, 0, )) &gt; 0)
      {
        (buffer, iBytes, 0);
      }
      ();
      if ()
      {
        ();
      }
      if (!(iReplyCode == 226 || iReplyCode == 250))
      {
        ReadReply();
        if (!(iReplyCode == 226 || iReplyCode == 250))
        {
          throw new IOException((4));
        }
      }
    }
 
 
    /// &lt;summary&gt;
    /// Upload a file    /// &lt;/summary&gt;
    /// <param name="strFileName">Local file name</param>    public void PutByGuid(string strFileName, string strGuid)
    {
      if (!bConnected)
      {
        Connect();
      }
      string str = (0, ("\\"));
      string strTypeName = (("."));
      strGuid = str + "\\" + strGuid;
      (strFileName, strGuid);
      (strGuid, );
      Socket socketData = CreateDataSocket();
      SendCommand("STOR " + (strGuid));
      if (!(iReplyCode == 125 || iReplyCode == 150))
      {
        throw new IOException((4));
      }
      FileStream input = new FileStream(strGuid, , , );
      int iBytes = 0;
      while ((iBytes = (buffer, 0, )) &gt; 0)
      {
        (buffer, iBytes, 0);
      }
      ();
      (strGuid);
      if ()
      {
        ();
      }
      if (!(iReplyCode == 226 || iReplyCode == 250))
      {
        ReadReply();
        if (!(iReplyCode == 226 || iReplyCode == 250))
        {
          throw new IOException((4));
        }
      }
    }
    #endregion
 
    #region directory operation    /// &lt;summary&gt;
    /// Create a directory    /// &lt;/summary&gt;
    /// <param name="strDirName">Directory name</param>    public void MkDir(string strDirName)
    {
      if (!bConnected)
      {
        Connect();
      }
      SendCommand("MKD " + strDirName);
      if (iReplyCode != 257)
      {
        throw new IOException((4));
      }
    }
 
    /// &lt;summary&gt;
    /// Delete the directory    /// &lt;/summary&gt;
    /// <param name="strDirName">Directory name</param>    public void RmDir(string strDirName)
    {
      if (!bConnected)
      {
        Connect();
      }
      SendCommand("RMD " + strDirName);
      if (iReplyCode != 250)
      {
        throw new IOException((4));
      }
    }
 
    /// &lt;summary&gt;
    /// Change directory    /// &lt;/summary&gt;
    /// <param name="strDirName">New working directory name</param>    public void ChDir(string strDirName)
    {
      if ((".") || (""))
      {
        return;
      }
      if (!bConnected)
      {
        Connect();
      }
      SendCommand("CWD " + strDirName);
      if (iReplyCode != 250)
      {
        throw new IOException((4));
      }
       = strDirName;
    }
    #endregion
 
    #region internal functions    /// &lt;summary&gt;
    /// Record a line of response strings in strReply and strMsg, and record the response code in iReplyCode    /// &lt;/summary&gt;
    private void ReadReply()
    {
      strMsg = "";
      strReply = ReadLine();
      iReplyCode = ((0, 3));
    }
 
    /// &lt;summary&gt;
    /// Establish a socket for data connection    /// &lt;/summary&gt;
    /// <returns>Data connection socket</returns>    private Socket CreateDataSocket()
    {
      SendCommand("PASV");
      if (iReplyCode != 227)
      {
        throw new IOException((4));
      }
      int index1 = ('(');
      int index2 = (')');
      string ipData = (index1 + 1, index2 - index1 - 1);
      int[] parts = new int[6];
      int len = ;
      int partCount = 0;
      string buf = "";
      for (int i = 0; i &lt; len &amp;&amp; partCount &lt;= 6; i++)
      {
        char ch = ((i, 1));
        if ((ch))
          buf += ch;
        else if (ch != ',')
        {
          throw new IOException("Malformed PASV strReply: " + strReply);
        }
        if (ch == ',' || i + 1 == len)
        {
          try
          {
            parts[partCount++] = (buf);
            buf = "";
          }
          catch (Exception)
          {
            throw new IOException("Malformed PASV strReply: " + strReply);
          }
        }
      }
      string ipAddress = parts[0] + "." + parts[1] + "." + parts[2] + "." + parts[3];
      int port = (parts[4] &lt;&lt; 8) + parts[5];
      Socket s = new Socket(, , );
      IPEndPoint ep = new IPEndPoint((ipAddress), port);
      try
      {
        (ep);
      }
      catch (Exception)
      {
        throw new IOException("Cannot connect to ftp server");
      }
      return s;
    }
 
    /// &lt;summary&gt;
    /// Close the socket connection (used before logging in)    /// &lt;/summary&gt;
    private void CloseSocketConnect()
    {
      lock (obj)
      {
        if (socketControl != null)
        {
          ();
          socketControl = null;
        }
        bConnected = false;
      }
    }
 
    /// &lt;summary&gt;
    /// Read all strings returned by Socket    /// &lt;/summary&gt;
    /// <returns>Serial containing answer code</returns>    private string ReadLine()
    {
      lock (obj)
      {
        while (true)
        {
          int iBytes = (buffer, , 0);
          strMsg += (buffer, 0, iBytes);
          if (iBytes &lt; )
          {
            break;
          }
        }
      }
      char[] seperator = { '\n' };
      string[] mess = (seperator);
      if ( &gt; 2)
      {
        strMsg = mess[ - 2];
      }
      else
      {
        strMsg = mess[0];
      }
      if (!(3, 1).Equals(" ")) //The correct return string is to use the answer code (such as starting with 220, followed by a space, and then the greeting string)      {
        return ReadLine();
      }
      return strMsg;
    }
 
    /// &lt;summary&gt;
    /// Send command and get the answer code and the last line of the answer string    /// &lt;/summary&gt;
    /// <param name="strCommand">Command</param>    public void SendCommand(String strCommand)
    {
      lock (obj)
      {
        Byte[] cmdBytes = ((strCommand + "\r\n").ToCharArray());
        (cmdBytes, , 0);
        (500);
        ReadReply();
      }
    }
    #endregion
  }
}

Method 2:

using System;
using ;
using ;
using ;
using ;
namespace FtpLib
{
public class FTPFactory
{
  private string
  remoteHost, remotePath, remoteUser, remotePass, mes;
  private int remotePort, bytes;
  private Socket clientSocket;
  private int retValue;
  private Boolean debug;
  private Boolean logined;
  private string reply;
  private static int BLOCK_SIZE = 512;
  Byte[] buffer = new Byte[BLOCK_SIZE];
  Encoding ASCII = ;
  public FTPFactory()
  {
    remoteHost = "localhost";
    remotePath = ".";
    remoteUser = "anonymous";
    remotePass = "";
    remotePort = 21;
    debug = false;
    logined = false;
  }
  ///

  /// Set the name of the FTP server to connect to.

  ///

  /// Server name

  public void setRemoteHost(string remoteHost)
  {
     = remoteHost;
  }
  ///

  /// Return the name of the current FTP server.

  ///

  /// Server name

  public string getRemoteHost()
  {
    return remoteHost;
  }
  ///

  /// Set the port number to use for FTP.

  ///

  /// Port number

  public void setRemotePort(int remotePort)
  {
     = remotePort;
  }
  ///

  /// Return the current port number.

  ///

  /// Current port number

  public int getRemotePort()
  {
    return remotePort;
  }
  ///

  /// Set the remote directory path.

  ///

  /// The remote directory path

  public void setRemotePath(string remotePath)
  {
     = remotePath;
  }
  ///

  /// Return the current remote directory path.

  ///

  /// The current remote directory path.

  public string getRemotePath()
  {
    return remotePath;
  }
  ///

  /// Set the user name to use for logging into the remote server.

  ///

  /// Username

  public void setRemoteUser(string remoteUser)
  {
     = remoteUser;
  }
  ///

  /// Set the password to user for logging into the remote server.

  ///

  /// Password

  public void setRemotePass(string remotePass)
  {
     = remotePass;
  }
  ///

  /// Return a string array containing the remote directory's file list.

  ///

  ///

  ///

  public string[] getFileList(string mask)
  {
    if (!logined)
    {
      login();
    }
    Socket cSocket = createDataSocket();
    sendCommand("NLST " + mask);
    if (!(retValue == 150 || retValue == 125))
    {
      throw new IOException((4));
    }
    mes = "";
    while (true)
    {
      int bytes = (buffer, , 0);
      mes += (buffer, 0, bytes);
      if (bytes < )
      {
        break;
      }
    }
    char[] seperator = { '\n' };
    string[] mess = (seperator);
    ();
    readReply();
    if (retValue != 226)
    {
      throw new IOException((4));
    }
    return mess;
  }
  ///

  /// Return the size of a file.

  ///

  ///

  ///

  public long getFileSize(string fileName)
  {
    if (!logined)
    {
      login();
    }
    sendCommand("SIZE " + fileName);
    long size = 0;
    if (retValue == 213)
    {
      size = ((4));
    }
    else
    {
      throw new IOException((4));
    }
    return size;
  }
  ///

  /// Login to the remote server.

  ///

  public void login()
  {
    clientSocket = new
    Socket(, , );
    IPEndPoint ep = new
    IPEndPoint((remoteHost).AddressList[0], remotePort);
    try
    {
      (ep);
    }
    catch (Exception)
    {
      throw new IOException("Couldn't connect to remote server");
    }
    readReply();
    if (retValue != 220)
    {
      close();
      throw new IOException((4));
    }
    if (debug)
      ("USER " + remoteUser);
    sendCommand("USER " + remoteUser);
    if (!(retValue == 331 || retValue == 230))
    {
      cleanup();
      throw new IOException((4));
    }
    if (retValue != 230)
    {
      if (debug)
        ("PASS xxx");
      sendCommand("PASS " + remotePass);
      if (!(retValue == 230 || retValue == 202))
      {
        cleanup();
        throw new IOException((4));
      }
    }
    logined = true;
    ("Connected to " + remoteHost);
    chdir(remotePath);
  }
  ///

  /// If the value of mode is true, set binary mode for downloads.

  /// Else, set Ascii mode.

  ///

  ///

  public void setBinaryMode(Boolean mode)
  {
    if (mode)
    {
      sendCommand("TYPE I");
    }
    else
    {
      sendCommand("TYPE A");
    }
    if (retValue != 200)
    {
      throw new IOException((4));
    }
  }
  ///

  /// Download a file to the Assembly's local directory,

  /// keeping the same file name.

  ///

  ///

  public void download(string remFileName)
  {
    download(remFileName, "", false);
  }
  ///

  /// Download a remote file to the Assembly's local directory,

  /// keeping the same file name, and set the resume flag.

  ///

  ///

  ///

  public void download(string remFileName, Boolean resume)
  {
    download(remFileName, "", resume);
  }
  ///

  /// Download a remote file to a local file name which can include

  /// a path. The local file name will be created or overwritten,

  /// but the path must exist.

  ///

  ///

  ///

  public void download(string remFileName, string locFileName)
  {
    download(remFileName, locFileName, false);
  }
  ///

  /// Download a remote file to a local file name which can include

  /// a path, and set the resume flag. The local file name will be

  /// created or overwritten, but the path must exist.

  ///

  ///

  ///

  ///

  public void download(string remFileName, string
  locFileName, Boolean resume)
  {
    if (!logined)
    {
      login();
    }
    setBinaryMode(true);
    ("Downloading file " + remFileName + " from " + remoteHost + "/" + remotePath);
    if ((""))
    {
      locFileName = remFileName;
    }
    if (!(locFileName))
    {
      Stream st = (locFileName);
      ();
    }
    FileStream output = new
    FileStream(locFileName, );
    Socket cSocket = createDataSocket();
    long offset = 0;
    if (resume)
    {
      offset = ;
      if (offset > 0)
      {
        sendCommand("REST " + offset);
        if (retValue != 350)
        {
          //throw new IOException((4));

          //Some servers may not support resuming.

          offset = 0;
        }
      }
      if (offset > 0)
      {
        if (debug)
        {
          ("seeking to " + offset);
        }
        long npos = (offset, );
        ("new pos=" + npos);
      }
    }
    sendCommand("RETR " + remFileName);
    if (!(retValue == 150 || retValue == 125) )
    {
      throw new IOException((4));
    }
    while (true)
    {
      bytes = (buffer, , 0);
      (buffer, 0, bytes);
      if (bytes <= 0)
      {
        break;
      }
    }
    ();
    if ()
    {
      ();
    }
    ("");
    readReply();
    if (!(retValue == 226 || retValue == 250))
    {
      throw new IOException((4));
    }
  }
  ///

  /// Upload a file.

  ///

  ///

  public void upload(string fileName)
  {
    upload(fileName, false);
  }
  ///

  /// Upload a file and set the resume flag.

  ///

  ///

  ///

  public void upload(string fileName, Boolean resume)
  {
    if (!logined)
    {
      login();
    }
    Socket cSocket = createDataSocket();
    long offset = 0;
    if (resume)
    {
      try
      {
        setBinaryMode(true);
        offset = getFileSize(fileName);
      }
      catch (Exception)
      {
        offset = 0;
      }
    }
    if (offset > 0)
    {
      sendCommand("REST " + offset);
      if (retValue != 350)
      {
        //throw new IOException((4));

        //Remote server may not support resuming.

        offset = 0;
      }
    }
    sendCommand("STOR " + (fileName));
    if (!(retValue == 125 || retValue == 150))
    {
      throw new IOException((4));
    }
    // open input stream to read source file

    FileStream input = new
    FileStream(fileName, );
    if (offset != 0)
    {
      if (debug)
      {
        ("seeking to " + offset);
      }
      (offset, );
    }
    ("Uploading file " + fileName + " to " + remotePath);
    while ((bytes = (buffer, 0, )) > 0)
    {
      (buffer, bytes, 0);
    }
    ();
    ("");
    if ()
    {
      ();
    }
    readReply();
    if (!(retValue == 226 || retValue == 250))
    {
      throw new IOException((4));
    }
  }
  ///

  /// Delete a file from the remote FTP server.

  ///

  ///

  public void deleteRemoteFile(string fileName)
  {
    if (!logined)
    {
      login();
    }
    sendCommand("DELE " + fileName);
    if (retValue != 250)
    {
      throw new IOException((4));
    }
  }
  ///

  /// Rename a file on the remote FTP server.

  ///

  ///

  ///

  public void renameRemoteFile(string oldFileName, string
  newFileName)
  {
    if (!logined)
    {
      login();
    }
    sendCommand("RNFR " + oldFileName);
    if (retValue != 350)
    {
      throw new IOException((4));
    }
    // known problem

    // rnto will not take care of existing file.

    // . It will overwrite if newFileName exist

    sendCommand("RNTO " + newFileName);
    if (retValue != 250)
    {
      throw new IOException((4));
    }
  }
  ///

  /// Create a directory on the remote FTP server.

  ///

  ///

  public void mkdir(string dirName)
  {
    if (!logined)
    {
      login();
    }
    sendCommand("MKD " + dirName);
    if (retValue != 257)
    {
      throw new IOException((4));
    }
  }
  ///

  /// Delete a directory on the remote FTP server.

  ///

  ///

  public void rmdir(string dirName)
  {
    if (!logined)
    {
      login();
    }
    sendCommand("RMD " + dirName);
    if (retValue != 250)
    {
      throw new IOException((4));
    }
  }
  ///

  /// Change the current working directory on the remote FTP server.

  ///

  ///

  public void chdir(string dirName)
  {
    if (("."))
    {
      return;
    }
    if (!logined)
    {
      login();
    }
    sendCommand("CWD " + dirName);
    if (retValue != 250)
    {
      throw new IOException((4));
    }
     = dirName;
    ("Current directory is " + remotePath);
  }
  ///

  /// Close the FTP connection.

  ///

  public void close()
  {
    if (clientSocket != null)
    {
      sendCommand("QUIT");
    }
    cleanup();
    ("Closing...");
  }
  ///

  /// Set debug mode.

  ///

  ///

  public void setDebug(Boolean debug)
  {
     = debug;
  }
  private void readReply()
  {
    mes = "";
    reply = readLine();
    retValue = ((0, 3));
  }
  private void cleanup()
  {
    if (clientSocket != null)
    {
      ();
      clientSocket = null;
    }
    logined = false;
  }
  private string readLine()
  {
    while (true)
    {
      bytes = (buffer, , 0);
      mes += (buffer, 0, bytes);
      if (bytes < )
      {
        break;
      }
    }
    char[] seperator = { '\n' };
    string[] mess = (seperator);
    if ( > 2)
    {
      mes = mess[ - 2];
    }
    else
    {
      mes = mess[0];
    }
    if (!(3, 1).Equals(" "))
    {
      return readLine();
    }
    if (debug)
    {
      for (int k = 0; k <  - 1; k++)
      {
        (mess[k]);
      }
    }
    return mes;
  }
  private void sendCommand(String command)
  {
    Byte[] cmdBytes =
      //((command + "\r\n").ToCharArray());

    ((command + "\r\n").ToCharArray()); 
    (cmdBytes, , 0);
    readReply();
  }
  private Socket createDataSocket()
  {
    sendCommand("PASV");
    if (retValue != 227)
    {
      throw new IOException((4));
    }
    int index1 = ('(');
    int index2 = (')');
    string ipData =
    (index1 + 1, index2 - index1 - 1);
    int[] parts = new int[6];
    int len = ;
    int partCount = 0;
    string buf = "";
    for (int i = 0; i < len && partCount <= 6; i++)
    {
      char ch = ((i, 1));
      if ((ch))
        buf += ch;
      else if (ch != ',')
      {
        throw new IOException("Malformed PASV reply: " +
        reply);
      }
      if (ch == ',' || i + 1 == len)
      {
        try
        {
          parts[partCount++] = (buf);
          buf = "";
        }
        catch (Exception)
        {
          throw new IOException("Malformed PASV reply: " +
          reply);
        }
      }
    }
    string ipAddress = parts[0] + "." + parts[1] + "." +
    parts[2] + "." + parts[3];
    int port = (parts[4] << 8) + parts[5];
    Socket s = new
    Socket(, , );
    IPEndPoint ep = new
    IPEndPoint((ipAddress).AddressList[0], port);
    try
    {
      (ep);
    }
    catch (Exception)
    {
      throw new IOException("Can't connect to remote server");
    }
    return s;
  }
}
}

The above is the entire content of this article, I hope you like it.