C implements custom FTP operations that encapsulate class instances

  • 2021-01-25 07:52:49
  • OfStack

This article illustrates C#'s implementation of a custom FTP operation wrapper class. Share with you for your reference. The details are as follows:

This C# class encapsulates common FTP operations, including connecting to the ftp server, listing directories and files on the server, downloading files from ftp, uploading files to the ftp server, and so on


using System;
using System.Text;
using System.IO;
namespace DotNet.Utilities
{
  public class FTPOperater
  {
    #region  attribute 
    private FTPClient ftp;
    /// <summary>
    ///  global FTP Access to the variable 
    /// </summary>
    public FTPClient Ftp
    {
      get { return ftp; }
      set { ftp = value; }
    }
    private string _server;
    /// <summary>
    /// Ftp The server 
    /// </summary>
    public string Server
    {
      get { return _server; }
      set { _server = value; }
    }
    private string _User;
    /// <summary>
    /// Ftp The 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 a list of files 
    /// </summary>
    /// <returns></returns>
    public string[] GetList(string strPath)
    {
      if (ftp == null) ftp = this.getFtpClient();
      ftp.Connect();
      ftp.ChDir(strPath);
      return ftp.Dir("*");
    }
    /// <summary>
    ///  The download file 
    /// </summary>
    /// <param name="ftpFolder">ftp directory </param>
    /// <param name="ftpFileName">ftp The 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 = this.getFtpClient();
        if (!ftp.Connected)
        {
          ftp.Connect();
          ftp.ChDir(ftpFolder);
        }
        ftp.Get(ftpFileName, localFolder, localFileName);
        return true;
      }
      catch
      {
        try
        {
          ftp.DisConnect();
          ftp = null;
        }
        catch { ftp = null; }
        return false;
      }
    }
    /// <summary>
    ///  Modify the file 
    /// </summary>
    /// <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 of the application ";
      path = path.Substring(0, path.LastIndexOf("\\"));
      try
      {
        FileStream fsFile = new FileStream(ftpFolder + "\\" + ftpFileName, FileMode.Open);
        FileStream fsFileWrite = new FileStream(localFolder + "\\" + localFileName, FileMode.Create);
        StreamReader sr = new StreamReader(fsFile);
        StreamWriter sw = new StreamWriter(fsFileWrite);
        sr.BaseStream.Seek(0, SeekOrigin.Begin);
        while (sr.Peek() > -1)
        {
          sLine = sr.ReadToEnd();
        }
        string[] arStr = sLine.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < arStr.Length - 1; i++)
        {
          sResult += BscInfo + "," + arStr[i].Trim() + "\n";
        }
        sr.Close();
        byte[] connect = new UTF8Encoding(true).GetBytes(sResult);
        fsFileWrite.Write(connect, 0, connect.Length);
        fsFileWrite.Flush();
        sw.Close();
        fsFile.Close();
        fsFileWrite.Close();
        return true;
      }
      catch (Exception e)
      {
        return false;
      }
    }
    /// <summary>
    ///  Delete the file 
    /// </summary>
    /// <param name="ftpFolder">ftp directory </param>
    /// <param name="ftpFileName">ftp The file name </param>
    public bool DelFile(string ftpFolder, string ftpFileName)
    {
      try
      {
        if (ftp == null) ftp = this.getFtpClient();
        if (!ftp.Connected)
        {
          ftp.Connect();
          ftp.ChDir(ftpFolder);
        }
        ftp.Delete(ftpFileName);
        return true;
      }
      catch
      {
        return false;
      }
    }
    /// <summary>
    ///  Upload a file 
    /// </summary>
    /// <param name="ftpFolder">ftp directory </param>
    /// <param name="ftpFileName">ftp The file name </param>
    public bool PutFile(string ftpFolder, string ftpFileName)
    {
      try
      {
        if (ftp == null) ftp = this.getFtpClient();
        if (!ftp.Connected)
        {
          ftp.Connect();
          ftp.ChDir(ftpFolder);
        }
        ftp.Put(ftpFileName);
        return true;
      }
      catch
      {
        return false;
      }
    }
    /// <summary>
    ///  The download file 
    /// </summary>
    /// <param name="ftpFolder">ftp directory </param>
    /// <param name="ftpFileName">ftp The 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 = this.getFtpClient();
        if (!ftp.Connected)
        {
          ftp.Connect();
          ftp.ChDir(ftpFolder);
        }
        ftp.GetNoBinary(ftpFileName, localFolder, localFileName);
        return true;
      }
      catch
      {
        try
        {
          ftp.DisConnect();
          ftp = null;
        }
        catch
        {
          ftp = null;
        }
        return false;
      }
    }
    /// <summary>
    ///  get FTP Upper file information 
    /// </summary>
    /// <param name="ftpFolder">FTP directory </param>
    /// <param name="ftpFileName">ftp The file name </param>
    public string GetFileInfo(string ftpFolder, string ftpFileName)
    {
      string strResult = "";
      try
      {
        if (ftp == null) ftp = this.getFtpClient();
        if (ftp.Connected) ftp.DisConnect();
        ftp.Connect();
        ftp.ChDir(ftpFolder);
        strResult = ftp.GetFileInfo(ftpFileName);
        return strResult;
      }
      catch
      {
        return "";
      }
    }
    /// <summary>
    ///  test FTP Whether the server can be logged in 
    /// </summary>
    public bool CanConnect()
    {
      if (ftp == null) ftp = this.getFtpClient();
      try
      {
        ftp.Connect();
        ftp.DisConnect();
        return true;
      }
      catch
      {
        return false;
      }
    }
    /// <summary>
    ///  get FTP Upper file information 
    /// </summary>
    /// <param name="ftpFolder">FTP directory </param>
    /// <param name="ftpFileName">ftp The file name </param>
    public string GetFileInfoConnected(string ftpFolder, string ftpFileName)
    {
      string strResult = "";
      try
      {
        if (ftp == null) ftp = this.getFtpClient();
        if (!ftp.Connected)
        {
          ftp.Connect();
          ftp.ChDir(ftpFolder);
        }
        strResult = ftp.GetFileInfo(ftpFileName);
        return strResult;
      }
      catch
      {
        return "";
      }
    }
    /// <summary>
    ///  Get a list of files 
    /// </summary>
    /// <param name="ftpFolder">FTP directory </param>
    /// <returns>FTP The wildcard no. </returns>
    public string[] GetFileList(string ftpFolder, string strMask)
    {
      string[] strResult;
      try
      {
        if (ftp == null) ftp = this.getFtpClient();
        if (!ftp.Connected)
        {
          ftp.Connect();
          ftp.ChDir(ftpFolder);
        }
        strResult = ftp.Dir(strMask);
        return strResult;
      }
      catch
      {
        return null;
      }
    }
    /// <summary>
    /// get FTP Transfer objects 
    /// </summary>
    public FTPClient getFtpClient()
    {
      FTPClient ft = new FTPClient();
      ft.RemoteHost = this.Server;
      ft.RemoteUser = this.User;
      ft.RemotePass = this.Pass;
      return ft;
    }
  }
}

I hope this article is helpful to your C# program design.


Related articles: