C# FTP 工具類
阿新 • • 發佈:2019-01-01
public class FtpTool { string ftpServerIP; //ftp伺服器地址 string ftpRemotePath; //ftp目錄 string ftpUserID; //登入使用者 string ftpPassword; //登入密碼 string ftpURI; //訪問資源地址 public FtpTool(string _ftpServerIP, string _ftpRemotePath, string _ftpUserID, string _ftpPassword) { ftpServerIP = _ftpServerIP; ftpRemotePath = _ftpRemotePath; ftpUserID = _ftpUserID; ftpPassword = _ftpPassword; ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/"; } /// <summary> /// 上傳檔案 /// </summary> /// <param name="filename">檔名</param> public void Upload(string filename) { FileInfo fileInfo = new FileInfo(filename); string uri = ftpURI + fileInfo.Name; FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.KeepAlive = false; reqFTP.Method = WebRequestMethods.Ftp.UploadFile; reqFTP.UseBinary = true; reqFTP.ContentLength = fileInfo.Length; int buffLength = 2048; byte[] buff = new byte[buffLength]; int contentLen; FileStream fs = fileInfo.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) { Console.WriteLine("出現異常:" + ex.Message); } } /// <summary> /// 下載檔案 /// </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, FileMode.Create); reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName)); reqFtp.Method = WebRequestMethods.Ftp.DownloadFile; reqFtp.Credentials = new NetworkCredential(ftpUserID, ftpPassword); FtpWebResponse response = (FtpWebResponse)reqFtp.GetResponse(); Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int buffSize = 2048; int readCount; byte[] buffer = new byte[buffSize]; readCount = ftpStream.Read(buffer, 0, buffSize); while (readCount > 0) { outputStream.Write(buffer, 0, buffSize); readCount = ftpStream.Read(buffer, 0, buffSize); } ftpStream.Close(); outputStream.Close(); response.Close(); } catch (Exception ex) { Console.WriteLine("出現異常:" + ex.Message); } } /// <summary> /// 獲取檔案(含資料夾)列表,詳細資訊包括檔名,檔案大小,建立日期等 /// </summary> /// <returns></returns> public string[] GetFilesDetailList() { string[] downloadFiles; 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 reader = new StreamReader(response.GetResponseStream(), Encoding.Default); string 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) { downloadFiles = null; Console.WriteLine("異常:" + ex.Message); return downloadFiles; } } /// <summary> /// 獲取檔案列表 /// </summary> /// <param name="mask"></param> /// <returns></returns> public string[] GetFileList(string mask) { string[] downloadFiles; StringBuilder result = new StringBuilder(); FtpWebRequest reqFTP; try { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI)); reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; WebResponse response = reqFTP.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default); string line = reader.ReadLine(); while (line != null) { if (mask.Trim() != string.Empty && mask.Trim() != "*.*") { string mask_ = mask.Substring(0, mask.IndexOf("*")); if (line.Substring(0, mask_.Length) == mask_) { result.Append(line); result.Append("\n"); } } else { 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) { downloadFiles = null; if (ex.Message.Trim() != "遠端伺服器返回錯誤: (550) 檔案不可用(例如,未找到檔案,無法訪問檔案)。") { Console.WriteLine("異常:"+ ex.Message); } return downloadFiles; } } }