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 && > 0 ){ foreach( var file in filePaths ){ (Upload( file )); } } return (); } /// <summary> /// Upload file /// </summary> /// <param name="filename"></param> 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 --> " + ); } return ""; } /// <summary> /// download /// </summary> /// <param name="filePath"></param> /// <param name="fileName"></param> 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 > 0) { (buffer, 0, readCount); readCount = (buffer, 0, bufferSize); } (); (); (); } catch (Exception ex) { Insert_Standard_ErrorLog.Insert("FtpWeb", "Download Error --> " + ); } } /// <summary> /// Delete the file /// </summary> /// <param name="fileName"></param> 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 --> " + + " file name:" + fileName); } } /// <summary> /// Get the details in the current directory (including files and folders) /// </summary> /// <returns></returns> 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 --> " + ); return downloadFiles; } } /// <summary> /// Get the list of files in the current directory (files only) /// </summary> /// <returns></returns> 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 (() != && () != "*.*") { 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 --> " + ()); } return downloadFiles; } } /// <summary> /// Get the list of all folders in the current directory (folders only) /// </summary> /// <returns></returns> 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); } /// <summary> /// Determine whether the specified subdirectory exists in the current directory /// </summary> /// <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; } /// <summary> /// Determine whether the specified file exists in the current directory /// </summary> /// <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; } /// <summary> /// Create folder /// </summary> /// <param name="dirName"></param> 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 --> " + ); } } /// <summary> /// Get the specified file size /// </summary> /// <param name="filename"></param> /// <returns></returns> 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 --> " + ); } return fileSize; } /// <summary> /// Change the name /// </summary> /// <param name="currentFilename"></param> /// <param name="newFilename"></param> 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 --> " + ); } } /// <summary> /// Move files /// </summary> /// <param name="currentFilename"></param> /// <param name="newFilename"></param> public void MovieFile(string currentFilename, string newDirectory) { ReName(currentFilename, newDirectory); }
I hope this article will be helpful to everyone's C# programming.