SoFunction
Updated on 2025-03-08

C# implements custom FTP operation encapsulation class instance

This article describes the implementation of C# custom FTP operation encapsulation classes. Share it for your reference. The details are as follows:

This C# class encapsulates common operations of FTP, including connecting to ftp servers, directories and files on list servers, downloading files from ftp, uploading files to ftp servers, etc.

using System;
using ;
using ;
namespace 
{
  public class FTPOperater
  {
    #region properties    private FTPClient ftp;
    /// <summary>
    /// Global FTP access variables    /// </summary>
    public FTPClient Ftp
    {
      get { return ftp; }
      set { ftp = value; }
    }
    private string _server;
    /// <summary>
    /// Ftp server    /// </summary>
    public string Server
    {
      get { return _server; }
      set { _server = value; }
    }
    private string _User;
    /// <summary>
    /// Ftp user    /// </summary>
    public string User
    {
      get { return _User; }
      set { _User = value; }
    }
    private string _Pass;
    /// <summary>
    /// Ftp password    /// </summary>
    public string Pass
    {
      get { return _Pass; }
      set { _Pass = value; }
    }
    private string _FolderZJ;
    /// <summary>
    /// Ftp password    /// </summary>
    public string FolderZJ
    {
      get { return _FolderZJ; }
      set { _FolderZJ = value; }
    }
    private string _FolderWX;
    /// <summary>
    /// Ftp password    /// </summary>
    public string FolderWX
    {
      get { return _FolderWX; }
      set { _FolderWX = value; }
    }
    #endregion
    /// <summary>
    /// Get the file list    /// </summary>
    /// <returns></returns>
    public string[] GetList(string strPath)
    {
      if (ftp == null) ftp = ();
      ();
      (strPath);
      return ("*");
    }
    /// <summary>
    /// Download the file    /// </summary>
    /// <param name="ftpFolder">ftp directory</param>    /// <param name="ftpFileName">ftp file name</param>    /// <param name="localFolder">Local Directory</param>    /// <param name="localFileName">local file name</param>    public bool GetFile(string ftpFolder, string ftpFileName, string localFolder, string localFileName)
    {
      try
      {
        if (ftp == null) ftp = ();
        if (!)
        {
          ();
          (ftpFolder);
        }
        (ftpFileName, localFolder, localFileName);
        return true;
      }
      catch
      {
        try
        {
          ();
          ftp = null;
        }
        catch { ftp = null; }
        return false;
      }
    }
    /// &lt;summary&gt;
    /// Modify the file    /// &lt;/summary&gt;
    /// <param name="ftpFolder">Local Directory</param>    /// <param name="ftpFileName">local file name temp</param>    /// <param name="localFolder">Local Directory</param>    /// <param name="localFileName">local file name</param>    public bool AddMSCFile(string ftpFolder, string ftpFileName, string localFolder, string localFileName, string BscInfo)
    {
      string sLine = "";
      string sResult = "";
      string path = "Get the full path to which the application is located";
      path = (0, ("\\"));
      try
      {
        FileStream fsFile = new FileStream(ftpFolder + "\\" + ftpFileName, );
        FileStream fsFileWrite = new FileStream(localFolder + "\\" + localFileName, );
        StreamReader sr = new StreamReader(fsFile);
        StreamWriter sw = new StreamWriter(fsFileWrite);
        (0, );
        while (() &gt; -1)
        {
          sLine = ();
        }
        string[] arStr = (new string[] { "\n" }, );
        for (int i = 0; i &lt;  - 1; i++)
        {
          sResult += BscInfo + "," + arStr[i].Trim() + "\n";
        }
        ();
        byte[] connect = new UTF8Encoding(true).GetBytes(sResult);
        (connect, 0, );
        ();
        ();
        ();
        ();
        return true;
      }
      catch (Exception e)
      {
        return false;
      }
    }
    /// &lt;summary&gt;
    /// Delete the file    /// &lt;/summary&gt;
    /// <param name="ftpFolder">ftp directory</param>    /// <param name="ftpFileName">ftp file name</param>    public bool DelFile(string ftpFolder, string ftpFileName)
    {
      try
      {
        if (ftp == null) ftp = ();
        if (!)
        {
          ();
          (ftpFolder);
        }
        (ftpFileName);
        return true;
      }
      catch
      {
        return false;
      }
    }
    /// &lt;summary&gt;
    /// Upload file    /// &lt;/summary&gt;
    /// <param name="ftpFolder">ftp directory</param>    /// <param name="ftpFileName">ftp file name</param>    public bool PutFile(string ftpFolder, string ftpFileName)
    {
      try
      {
        if (ftp == null) ftp = ();
        if (!)
        {
          ();
          (ftpFolder);
        }
        (ftpFileName);
        return true;
      }
      catch
      {
        return false;
      }
    }
    /// &lt;summary&gt;
    /// Download the file    /// &lt;/summary&gt;
    /// <param name="ftpFolder">ftp directory</param>    /// <param name="ftpFileName">ftp file name</param>    /// <param name="localFolder">Local Directory</param>    /// <param name="localFileName">local file name</param>    public bool GetFileNoBinary(string ftpFolder, string ftpFileName, string localFolder, string localFileName)
    {
      try
      {
        if (ftp == null) ftp = ();
        if (!)
        {
          ();
          (ftpFolder);
        }
        (ftpFileName, localFolder, localFileName);
        return true;
      }
      catch
      {
        try
        {
          ();
          ftp = null;
        }
        catch
        {
          ftp = null;
        }
        return false;
      }
    }
    /// &lt;summary&gt;
    /// Get the file information on FTP    /// &lt;/summary&gt;
    /// <param name="ftpFolder">FTP Directory</param>    /// <param name="ftpFileName">ftp file name</param>    public string GetFileInfo(string ftpFolder, string ftpFileName)
    {
      string strResult = "";
      try
      {
        if (ftp == null) ftp = ();
        if () ();
        ();
        (ftpFolder);
        strResult = (ftpFileName);
        return strResult;
      }
      catch
      {
        return "";
      }
    }
    /// &lt;summary&gt;
    /// Test whether the FTP server can be logged in    /// &lt;/summary&gt;
    public bool CanConnect()
    {
      if (ftp == null) ftp = ();
      try
      {
        ();
        ();
        return true;
      }
      catch
      {
        return false;
      }
    }
    /// &lt;summary&gt;
    /// Get the file information on FTP    /// &lt;/summary&gt;
    /// <param name="ftpFolder">FTP Directory</param>    /// <param name="ftpFileName">ftp file name</param>    public string GetFileInfoConnected(string ftpFolder, string ftpFileName)
    {
      string strResult = "";
      try
      {
        if (ftp == null) ftp = ();
        if (!)
        {
          ();
          (ftpFolder);
        }
        strResult = (ftpFileName);
        return strResult;
      }
      catch
      {
        return "";
      }
    }
    /// &lt;summary&gt;
    /// Get the file list    /// &lt;/summary&gt;
    /// <param name="ftpFolder">FTP Directory</param>    /// <returns>FTP wildcard symbol</returns>    public string[] GetFileList(string ftpFolder, string strMask)
    {
      string[] strResult;
      try
      {
        if (ftp == null) ftp = ();
        if (!)
        {
          ();
          (ftpFolder);
        }
        strResult = (strMask);
        return strResult;
      }
      catch
      {
        return null;
      }
    }
    /// &lt;summary&gt;
    ///Get the FTP transmission object    /// &lt;/summary&gt;
    public FTPClient getFtpClient()
    {
      FTPClient ft = new FTPClient();
       = ;
       = ;
       = ;
      return ft;
    }
  }
}

I hope this article will be helpful to everyone's C# programming.