SoFunction
Updated on 2025-03-06

C# operation complete example of ftp class

This article describes the C# operation of the ftp class. Share it for your reference. The details are as follows:

using System;
using ;
using ;
using ;
using ;
using ;
namespace FtpTest1
{
 public class FtpWeb
 {
 string ftpServerIP;
 string ftpRemotePath;
 string ftpUserID;
 string ftpPassword;
 string ftpURI;
 /// <summary>
 /// Connect to FTP /// </summary>
 /// <param name="FtpServerIP">FTP connection address</param> /// <param name="FtpRemotePath">Specify the current directory after the FTP connection is successful. If it is not specified, the default is the root directory</param> /// <param name="FtpUserID">Username</param> /// <param name="FtpPassword">Password</param> public FtpWeb(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
 {
  ftpServerIP = FtpServerIP;
  ftpRemotePath = FtpRemotePath;
  ftpUserID = FtpUserID;
  ftpPassword = FtpPassword;
  ftpURI = "ftp://" + ftpServerIP + "/" ;
 }
 static void Main() {
  //string file = "c:\\";
  //FileInfo fileInf = new FileInfo(file);
  //if (!)
  //{
  // (file + " no exists");
  //}
  //else {
  // ("yes");
  //}
  //();
  FtpWeb fw = new FtpWeb("121.11.65.10", "", "aa1", "aa");
  string[] filePaths = { "c:\\aq3.gif1", "c:\\aq2.gif1", "c:\\bsmain_runtime.log" };
  ((filePaths));
  ();
 }
 //Upload file public string UploadFile( string[] filePaths ) {
  StringBuilder sb = new StringBuilder();
  if ( filePaths != null &amp;&amp;  &gt; 0 ){
  foreach( var file in filePaths ){
   (Upload( file ));
  }
  }
  return ();
 }
  /// &lt;summary&gt;
 /// Upload file /// &lt;/summary&gt;
 /// &lt;param name="filename"&gt;&lt;/param&gt;
 private string Upload(string filename)
 {
  FileInfo fileInf = new FileInfo(filename);
  if ( ! ){
  return filename + "Not exists!\n";
  }
  string uri = ftpURI + ;
  FtpWebRequest reqFTP;
  reqFTP = (FtpWebRequest)(new Uri(uri));
   = new NetworkCredential(ftpUserID, ftpPassword);
   = false;
   = ;
   = true;
   = false; //Choose active or passive mode  //Entering Passive Mode
   = ;
  int buffLength = 2048;
  byte[] buff = new byte[buffLength];
  int contentLen;
  FileStream fs = ();
  try
  {
  Stream strm = ();
  contentLen = (buff, 0, buffLength);
  while (contentLen != 0)
  {
   (buff, 0, contentLen);
   contentLen = (buff, 0, buffLength);
  }
  ();
  ();
  }
  catch (Exception ex)
  {
  return "Synchronous"+filename+"The server cannot be connected to it!\n";
  //Insert_Standard_ErrorLog.Insert("FtpWeb", "Upload Error --&gt; " + );
  }
  return "";
 }
 /// &lt;summary&gt;
 /// download /// &lt;/summary&gt;
 /// &lt;param name="filePath"&gt;&lt;/param&gt;
 /// &lt;param name="fileName"&gt;&lt;/param&gt;
 public void Download(string filePath, string fileName)
 {
  FtpWebRequest reqFTP;
  try
  {
  FileStream outputStream = new FileStream(filePath + "\\" + fileName, );
  reqFTP = (FtpWebRequest)(new Uri(ftpURI + fileName));
   = ;
   = true;
   = new NetworkCredential(ftpUserID, ftpPassword);
  FtpWebResponse response = (FtpWebResponse)();
  Stream ftpStream = ();
  long cl = ;
  int bufferSize = 2048;
  int readCount;
  byte[] buffer = new byte[bufferSize];
  readCount = (buffer, 0, bufferSize);
  while (readCount &gt; 0)
  {
   (buffer, 0, readCount);
   readCount = (buffer, 0, bufferSize);
  }
  ();
  ();
  ();
  }
  catch (Exception ex)
  {
  Insert_Standard_ErrorLog.Insert("FtpWeb", "Download Error --&gt; " + );
  }
 }
 /// &lt;summary&gt;
 /// Delete the file /// &lt;/summary&gt;
 /// &lt;param name="fileName"&gt;&lt;/param&gt;
 public void Delete(string fileName)
 {
  try
  {
  string uri = ftpURI + fileName;
  FtpWebRequest reqFTP;
  reqFTP = (FtpWebRequest)(new Uri(uri));
   = new NetworkCredential(ftpUserID, ftpPassword);
   = false;
   = ;
  string result = ;
  FtpWebResponse response = (FtpWebResponse)();
  long size = ;
  Stream datastream = ();
  StreamReader sr = new StreamReader(datastream);
  result = ();
  ();
  ();
  ();
  }
  catch (Exception ex)
  {
  Insert_Standard_ErrorLog.Insert("FtpWeb", "Delete Error --&gt; " +  + " file name:" + fileName);
  }
 }
 /// &lt;summary&gt;
 /// Get the details in the current directory (including files and folders) /// &lt;/summary&gt;
 /// &lt;returns&gt;&lt;/returns&gt;
 public string[] GetFilesDetailList()
 {
  string[] downloadFiles;
  try
  {
  StringBuilder result = new StringBuilder();
  FtpWebRequest ftp;
  ftp = (FtpWebRequest)(new Uri(ftpURI));
   = new NetworkCredential(ftpUserID, ftpPassword);
   = ;
  WebResponse response = ();
  StreamReader reader = new StreamReader(());
  string line = ();
  line = ();
  line = ();
  while (line != null)
  {
   (line);
   ("\n");
   line = ();
  }
  (().LastIndexOf("\n"), 1);
  ();
  ();
  return ().Split('\n');
  }
  catch (Exception ex)
  {
  downloadFiles = null;
  Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFilesDetailList Error --&gt; " + );
  return downloadFiles;
  }
 }
 /// &lt;summary&gt;
 /// Get the list of files in the current directory (files only) /// &lt;/summary&gt;
 /// &lt;returns&gt;&lt;/returns&gt;
 public string[] GetFileList(string mask)
 {
  string[] downloadFiles;
  StringBuilder result = new StringBuilder();
  FtpWebRequest reqFTP;
  try
  {
  reqFTP = (FtpWebRequest)(new Uri(ftpURI));
   = true;
   = new NetworkCredential(ftpUserID, ftpPassword);
   = ;
  WebResponse response = ();
  StreamReader reader = new StreamReader(());
  string line = ();
  while (line != null)
  {
   if (() !=  &amp;&amp; () != "*.*")
   {
   string mask_ = (0, ("*"));
   if ((0, mask_.Length) == mask_)
   {
    (line);
    ("\n");
   }
   }
   else
   {
   (line);
   ("\n");
   }
   line = ();
  }
  (().LastIndexOf('\n'), 1);
  ();
  ();
  return ().Split('\n');
  }
  catch (Exception ex)
  {
  downloadFiles = null;
  if (() != "Remote server returns an error: (550) File not available(For example,File not found,Unable to access the file)。")
  {
   Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFileList Error --&gt; " + ());
  }
  return downloadFiles;
  }
 }
 /// &lt;summary&gt;
 /// Get the list of all folders in the current directory (folders only) /// &lt;/summary&gt;
 /// &lt;returns&gt;&lt;/returns&gt;
 public string[] GetDirectoryList()
 {
  string[] drectory = GetFilesDetailList();
  string m = ;
  foreach (string str in drectory)
  {
  if (().Substring(0, 1).ToUpper() == "D")
  {
   m += (54).Trim() + "\n";
  }
  }
  char[] n = new char[] { '\n' };
  return (n);
 }
 /// &lt;summary&gt;
 /// Determine whether the specified subdirectory exists in the current directory /// &lt;/summary&gt;
 /// <param name="RemoteDirectoryName">Specified directory name</param> public bool DirectoryExist(string RemoteDirectoryName)
 {
  string[] dirList = GetDirectoryList();
  foreach (string str in dirList)
  {
  if (() == ())
  {
   return true;
  }
  }
  return false;
 }
 /// &lt;summary&gt;
 /// Determine whether the specified file exists in the current directory /// &lt;/summary&gt;
 /// <param name="RemoteFileName">Remote file name</param> public bool FileExist(string RemoteFileName)
 {
  string[] fileList = GetFileList("*.*");
  foreach (string str in fileList)
  {
  if (() == ())
  {
   return true;
  }
  }
  return false;
 }
 /// &lt;summary&gt;
 /// Create folder /// &lt;/summary&gt;
 /// &lt;param name="dirName"&gt;&lt;/param&gt;
 public void MakeDir(string dirName)
 {
  FtpWebRequest reqFTP;
  try
  {
  // dirName = name of the directory to create.
  reqFTP = (FtpWebRequest)(new Uri(ftpURI + dirName));
   = ;
   = true;
   = new NetworkCredential(ftpUserID, ftpPassword);
  FtpWebResponse response = (FtpWebResponse)();
  Stream ftpStream = ();
  ();
  ();
  }
  catch (Exception ex)
  {
  Insert_Standard_ErrorLog.Insert("FtpWeb", "MakeDir Error --&gt; " + );
  }
 }
 /// &lt;summary&gt;
 /// Get the specified file size /// &lt;/summary&gt;
 /// &lt;param name="filename"&gt;&lt;/param&gt;
 /// &lt;returns&gt;&lt;/returns&gt;
 public long GetFileSize(string filename)
 {
  FtpWebRequest reqFTP;
  long fileSize = 0;
  try
  {
  reqFTP = (FtpWebRequest)(new Uri(ftpURI + filename));
   = ;
   = true;
   = new NetworkCredential(ftpUserID, ftpPassword);
  FtpWebResponse response = (FtpWebResponse)();
  Stream ftpStream = ();
  fileSize = ;
  ();
  ();
  }
  catch (Exception ex)
  {
  Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFileSize Error --&gt; " + );
  }
  return fileSize;
 }
 /// &lt;summary&gt;
 /// Change the name /// &lt;/summary&gt;
 /// &lt;param name="currentFilename"&gt;&lt;/param&gt;
 /// &lt;param name="newFilename"&gt;&lt;/param&gt;
 public void ReName(string currentFilename, string newFilename)
 {
  FtpWebRequest reqFTP;
  try
  {
  reqFTP = (FtpWebRequest)(new Uri(ftpURI + currentFilename));
   = ;
   = newFilename;
   = true;
   = new NetworkCredential(ftpUserID, ftpPassword);
  FtpWebResponse response = (FtpWebResponse)();
  Stream ftpStream = ();
  ();
  ();
  }
  catch (Exception ex)
  {
  Insert_Standard_ErrorLog.Insert("FtpWeb", "ReName Error --&gt; " + );
  }
 }
 /// &lt;summary&gt;
 /// Move files /// &lt;/summary&gt;
 /// &lt;param name="currentFilename"&gt;&lt;/param&gt;
 /// &lt;param name="newFilename"&gt;&lt;/param&gt;
 public void MovieFile(string currentFilename, string newDirectory)
 {
  ReName(currentFilename, newDirectory);
 }

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