SoFunction
Updated on 2025-03-06

Implement a simple FTP operation tool based on C#

Implement functions

Implement the functions of uploading, downloading, renaming, refreshing, and deleting using FTP

Development Environment

Development Tools: Visual Studio 2013

.NET Framework version: 4.5

Implement code

  /*FTP operation public class*/
 
  private string FtpIp, FtpPort, FtpUser, FtpPwd, FtpUrl;
 
  private FTPUtil()
  {
 
  }
 
  public FTPUtil(string ftpIp, string ftpPort, string ftpUser, string ftpPwd)
  {
      FtpIp = ftpIp;
      FtpPort = ftpPort;
      FtpUser = ftpUser;
      FtpPwd = ftpPwd;
 
      FtpUrl = "ftp://" + ftpIp + ":" + ftpPort + "/";
  }
 
  private FtpWebRequest GetFtpWebRequest(string path, string method)
  {
      FtpWebRequest Ftp = (FtpWebRequest)(new Uri(FtpUrl + "/" + path));
       = new NetworkCredential(FtpUser, FtpPwd);
       = false;
       = true;
       = method;
      return Ftp;
  }
 
  /// <summary>
  /// Get all folders under the path  /// </summary>
  /// <param name="dirName"></param>
  /// <returns></returns>
  public List<FileModel> GetDirs(string dirName)
  {
      return GetAllFiles(dirName).FindAll(s =>  == "Folder");
  }
 
  /// <summary>
  /// Get all files under the path  /// </summary>
  /// <param name="dirName"></param>
  /// <returns></returns>
  public List<FileModel> GetFiles(string dirName)
  {
      return GetAllFiles(dirName).FindAll(s =>  == "document");
  }
 
  /// <summary>
  /// Get all items under the path  /// </summary>
  /// <param name="dirName"></param>
  /// <returns></returns>
  public List<FileModel> GetAllFiles(string dirName)
  {
      List<FileModel> fileList = new List<FileModel>();
      try
      {
          FtpWebRequest Ftp = GetFtpWebRequest(dirName, );
 
          using (WebResponse response = ())
          {
              using (StreamReader reader = new StreamReader((), Encoding.UTF8))
              {
                  string line = "";
                  while ((line = ()) != null)
                  {
                      (ConvertFile(line, dirName));
                  }
              }
          }
      }
      catch (Exception ex)
      {
          throw ex;
      }
      return fileList;
  }
 
  /// <summary>
  /// FTP file information conversion  /// </summary>
  /// <param name="value"></param>
  /// <param name="dirName"></param>
  /// <returns></returns>
  private FileModel ConvertFile(string value, string dirName)
  {
      string[] arr = (new string[] { " " },4, );
 
     FileModel model = new FileModel();
       = arr[0];
       = arr[1];
      if (arr[2] == "<DIR>")
      {
           = "Folder";
           = 0;
      }
      else
      {
           = "document";
           = Convert.ToInt64(arr[2]);
      }
       = arr[3];
       = dirName + "/" + ;
      return model;
  }
 
  /// <summary>
  /// Upload  /// </summary>
  /// <param name="fileName"></param>
  /// <param name="desFile"></param>
  public void Upload(string fileName, string desFile)
  {
      try
      {
          FileInfo fileInfo = new FileInfo(fileName);
 
          FtpWebRequest Ftp = GetFtpWebRequest(desFile, );
           = true;
           = ;
 
 
          int buffLength = 2048;
          byte[] buff = new byte[buffLength];
          int len = 0;
          using (FileStream fs = ())
          {
              using (Stream stream = ())
              {
                  while ((len = (buff, 0, buffLength)) != 0)
                  {
                      (buff, 0, buffLength);
                  }
              }
          }
      }
      catch (Exception ex)
      {
          throw ex;
      }
 
 
  }
 
  /// <summary>
  /// download  /// </summary>
  /// <param name="fileName"></param>
  /// <param name="desFile"></param>
  public void DownLoad(string fileName, string desFile)
  {
      try
      {
          FtpWebRequest Ftp = GetFtpWebRequest(fileName, );
           = true;
 
          FtpWebResponse response = (FtpWebResponse)();
          int buffLength = 2048;
          byte[] buff = new byte[buffLength];
          int len = 0;
          using (FileStream fs = new FileStream(desFile, ))
          {
              using (Stream stream = ())
              {
                  while ((len = (buff, 0, buffLength)) != 0)
                  {
                      (buff, 0, buffLength);
                  }
              }
          }
      }
      catch (Exception ex)
      {
          throw ex;
      }
  }
 
  /// <summary>
  /// Delete the file  /// </summary>
  /// <param name="fileName"></param>
  public void DeleteFile(string fileName)
  {
      try
      {
          FtpWebRequest Ftp = GetFtpWebRequest(fileName, );
 
          FtpWebResponse response = (FtpWebResponse)();
 
          using (Stream datastream = ())
          {
              using (StreamReader sr = new StreamReader(datastream))
              {
                  ();
              }
          }
      }
      catch (Exception ex)
      {
          throw ex;
      }
  }
 
 
  /// <summary>
  /// Rename  /// </summary>
  /// <param name="fileName"></param>
  /// <param name="newName"></param>
  public void ReName(string fileName, string newName)
  {
      try
      {
          FtpWebRequest Ftp = GetFtpWebRequest(fileName, );
           = newName;
           = true;
 
          FtpWebResponse response = (FtpWebResponse)();
 
          using (Stream datastream = ())
          {
              using (StreamReader sr = new StreamReader(datastream))
              {
                  ();
              }
          }
      }
      catch (Exception ex)
      {
          throw ex;
      }
  }

Realize the effect

FTP operation tool video demonstration

The above is the detailed content of implementing a simple FTP operation tool based on C#. For more information about C# FTP operation tool, please pay attention to my other related articles!