C # FTP operations, including uploading, downloading, deleting, and obtaining FTP file list folders,
C # ftp-reference required by this instance:
I. References
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net;using System.Text.RegularExpressions;
Ii. namespaces and fields
Namespace DotNet. utilities {public class FTPHelper {# region field string ftpURI; string ftpUserID; string ftpServerIP; string ftpPassword; string ftpRemotePath; # endregion // <summary> // connect to the FTP server // </summary> /// <param name = "FtpServerIP"> FTP connection address </param>/ // <param name = "FtpRemotePath"> specify the current directory after successful FTP connection, if this parameter is not specified, the root directory is used by default. </param> /// <param name = "FtpUserID"> User name </param> /// <param name = "FtpPassword"> password </param> public FTPHelper (string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword) {ftpServerIP = FtpServerIP; ftpRemotePath = FtpRemotePath; ftpUserID = FtpUserID; ftpPassword = FtpPassword; ftpURI = "ftp: // "+ ftpServerIP +"/"+ ftpRemotePath + "/";}
3. Upload the file instance code through ftp. You only need to pass the corresponding file name and call this method.
/// <Summary> /// Upload /// </summary> public void Upload (string filename) {FileInfo fileInf = new FileInfo (filename); FtpWebRequest reqFTP; reqFTP = (FtpWebRequest) FtpWebRequest. create (new Uri (ftpURI + fileInf. name); reqFTP. credentials = new NetworkCredential (ftpUserID, ftpPassword); reqFTP. method = WebRequestMethods. ftp. uploadFile; reqFTP. keepAlive = false; reqFTP. useBinary = true; reqFTP. contentL Ength = fileInf. length; int buffLength = 2048; byte [] buff = new byte [buffLength]; int contentLen; FileStream fs = fileInf. openRead (); try {Stream strm = reqFTP. getRequestStream (); contentLen = fs. read (buff, 0, buffLength); while (contentLen! = 0) {strm. write (buff, 0, contentLen); contentLen = fs. read (buff, 0, buffLength);} strm. close (); fs. close ();} catch (Exception ex) {throw new Exception (ex. message );}}
4. For ftp download instance code, you only need to pass the corresponding file path and file name. You can call this method to complete the download.
/// <Summary> /// Download /// </summary> public void Download (string filePath, string fileName) {try {FileStream outputStream = new FileStream (filePath + "\" + fileName, FileMode. create); FtpWebRequest reqFTP; reqFTP = (FtpWebRequest) FtpWebRequest. create (new Uri (ftpURI + fileName); reqFTP. credentials = new NetworkCredential (ftpUserID, ftpPassword); reqFTP. method = WebRequestMethods. ftp. downloadFile; reqFTP. useBinary = true; FtpWebResponse response = (FtpWebResponse) reqFTP. getResponse (); Stream ftpStream = response. getResponseStream (); long cl = response. contentLength; int bufferSize = 2048; int readCount; byte [] buffer = new byte [bufferSize]; readCount = ftpStream. read (buffer, 0, bufferSize); while (readCount> 0) {outputStream. write (buffer, 0, readCount); readCount = ftpStream. read (buffer, 0, bufferSize);} ftpStream. close (); outputStream. close (); response. close ();} catch (Exception ex) {throw new Exception (ex. message );}}
5. The instance code of ftp file deletion, which can be uploaded or downloaded, must be deleted. It is still simple. You can delete the file by passing in the file name.
/// <Summary> /// Delete a file /// </summary> public void Delete (string fileName) {try {FtpWebRequest reqFTP; reqFTP = (FtpWebRequest) FtpWebRequest. create (new Uri (ftpURI + fileName); reqFTP. credentials = new NetworkCredential (ftpUserID, ftpPassword); reqFTP. method = WebRequestMethods. ftp. deleteFile; reqFTP. keepAlive = false; string result = String. empty; FtpWebResponse response = (FtpWebResponse) reqFTP. getResponse (); long size = response. contentLength; Stream datastream = response. getResponseStream (); StreamReader sr = new StreamReader (datastream); result = sr. readToEnd (); sr. close (); datastream. close (); response. close ();} catch (Exception ex) {throw new Exception (ex. message );}}
6. Obtain the instance code of the list through ftp and obtain the details (including files and folders) under the current directory. This may not be used much, but it also needs to be understood.
/// <Summary> /// obtain the details of the current directory (including files and folders) /// </summary> public string [] GetFilesDetailList () {try {StringBuilder result = new StringBuilder (); FtpWebRequest ftp; ftp = (FtpWebRequest) FtpWebRequest. create (new Uri (ftpURI); ftp. credentials = new NetworkCredential (ftpUserID, ftpPassword); ftp. method = WebRequestMethods. ftp. listDirectoryDetails; WebResponse response = ftp. getResponse (); StreamReader rea Der = new StreamReader (response. getResponseStream (); string line = reader. readLine (); line = reader. readLine (); line = reader. readLine (); while (line! = Null) {result. append (line); result. append ("\ n"); line = reader. readLine ();} result. remove (result. toString (). lastIndexOf ("\ n"), 1); reader. close (); response. close (); return result. toString (). split ('\ n');} catch (Exception ex) {throw new Exception (ex. message );}}
7. Obtain the ftp file list (including folders) by using the FTP instance code)
/// <Summary> /// obtain the FTP file list (including folders) /// </summary> private string [] GetAllList (string url) {List <string> list = new List <string> (); FtpWebRequest req = (FtpWebRequest) WebRequest. create (new Uri (url); req. credentials = new NetworkCredential (ftpPassword, ftpPassword); req. method = WebRequestMethods. ftp. listDirectory; req. useBinary = true; req. usePassive = true; try {using (FtpWebResponse res = (FtpW EbResponse) req. GetResponse () {using (StreamReader sr = new StreamReader (res. GetResponseStream () {string s; while (s = sr. ReadLine ())! = Null) {list. Add (s) ;}}} catch (Exception ex) {throw (ex);} return list. ToArray ();}
8. How to Create a folder through ftp
/// <Summary> /// create a folder /// </summary> public void MakeDir (string dirName) {FtpWebRequest reqFTP; try {reqFTP = (FtpWebRequest) FtpWebRequest. create (new Uri (ftpURI + dirName); reqFTP. method = WebRequestMethods. ftp. makeDirectory; reqFTP. useBinary = true; reqFTP. credentials = new NetworkCredential (ftpUserID, ftpPassword); FtpWebResponse response = (FtpWebResponse) reqFTP. getResponse (); Stream ftpStream = response. getResponseStream (); ftpStream. close (); response. close () ;}catch (Exception ex ){}}
9: Specifies the file size for ftp. You only need to provide the file name.
/// <Summary> /// obtain the specified file size /// </summary> public long GetFileSize (string filename) {FtpWebRequest reqFTP; long fileSize = 0; try {reqFTP = (FtpWebRequest) FtpWebRequest. create (new Uri (ftpURI + filename); reqFTP. method = WebRequestMethods. ftp. getFileSize; reqFTP. useBinary = true; reqFTP. credentials = new NetworkCredential (ftpUserID, ftpPassword); FtpWebResponse response = (FtpWebResponse) reqFTP. getResponse (); Stream ftpStream = response. getResponseStream (); fileSize = response. contentLength; ftpStream. close (); response. close ();} catch (Exception ex) {} return fileSize ;}
10. Change the file name through ftp. Pass the current file name and the new file name.
/// <Summary> /// change the file name /// </summary> public void ReName (string currentFilename, string newFilename) {FtpWebRequest reqFTP; try {reqFTP = (FtpWebRequest) ftpWebRequest. create (new Uri (ftpURI + currentFilename); reqFTP. method = WebRequestMethods. ftp. rename; reqFTP. renameTo = newFilename; reqFTP. useBinary = true; reqFTP. credentials = new NetworkCredential (ftpUserID, ftpPassword); FtpWebResponse response = (FtpWebResponse) reqFTP. getResponse (); Stream ftpStream = response. getResponseStream (); ftpStream. close (); response. close () ;}catch (Exception ex ){}}
11: ftp mobile files are not used in many cases, but are often used in some cases. The current path and the new path are the correct ones. here we need to use the absolute path.
/// <Summary >/// move the file /// </summary> public void MovieFile (string currentFilename, string newDirectory) {ReName (currentFilename, newDirectory );}}}
Haha, I have summarized so much that I hope to provide some help to the people who need it. Of course there are other methods I will not list them one by one. I will work harder on multiple lines! Share with you