C#編寫的FTP上傳簡單工具類
C#編寫的FTP上傳簡單工具類
using System; using System.Net; using System.IO; using System.Text; using System.Net.Sockets; using System.Collections; using System.Collections.Generic;namespace WFDownLoad_ftp { /// <summary> /// FTP Client /// </summary> public class FTPClient {
#region 構造函數 /// <summary> /// 缺省構造函數 /// </summary> public FTPClient() { strRemoteHost = ""; strRemotePath = ""; strRemoteUser = ""; strRemotePass = ""; strRemotePort = 21; bConnected = false; } /// <summary> /// 構造函數 /// </summary> /// <param name="remoteHost"></param> /// <param name="remotePath"></param> /// <param name="remoteUser"></param> /// <param name="remotePass"></param> /// <param name="remotePort"></param> public FTPClient(string remoteHost, string remotePath, string remoteUser, string remotePass, int remotePort) { strRemoteHost = remoteHost; strRemotePath = remotePath; strRemoteUser = remoteUser; strRemotePass = remotePass; strRemotePort = remotePort; Connect(); } #endregion //public static void Main(string[] args) //{ // FTPClient fc = new FTPClient("172.22.1.15", "/bcgz/x25/awstod", "zxt", "123456", 21); // //fc.ChDir("/bcgz/x25/awstod"); // //Console.WriteLine(fc.RemotePath); // fc.Get("S5290900.A1053", @"F:\ttt", ""); // // fc.getawsFile("S5290400.A1053"); //} #region 登陸 /// <summary> /// FTP服務器IP地址 /// </summary> private string strRemoteHost; public string RemoteHost { get { return strRemoteHost; } set { strRemoteHost = value; } } /// <summary> /// FTP服務器端口 /// </summary> private int strRemotePort; public int RemotePort { get { return strRemotePort; } set { strRemotePort = value; } } /// <summary> /// 當前服務器目錄 /// </summary> private string strRemotePath; public string RemotePath { get { return strRemotePath; } set { strRemotePath = value; } } /// <summary> /// 登錄用戶賬號 /// </summary> private string strRemoteUser; public string RemoteUser { set { strRemoteUser = value; } } /// <summary> /// 用戶登錄密碼 /// </summary> private string strRemotePass; public string RemotePass { set { strRemotePass = value; } } /// <summary> /// 是否登錄 /// </summary> private Boolean bConnected; public bool Connected { get { return bConnected; } } #endregion #region 鏈接 /// <summary> /// 建立連接 /// </summary> public void Connect() { socketControl = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ep = new IPEndPoint(IPAddress.Parse(RemoteHost), strRemotePort); // 鏈接 try { socketControl.Connect(ep); } catch (Exception) { throw new IOException("Couldnt connect to remote server"); } // 獲取應答碼 ReadReply(); if (iReplyCode != 220) { DisConnect(); throw new IOException(strReply.Substring(4)); } // 登陸 SendCommand("USER " + strRemoteUser); if (!(iReplyCode == 331 || iReplyCode == 230)) { CloseSocketConnect();//關閉連接 throw new IOException(strReply.Substring(4)); } if (iReplyCode != 230) { SendCommand("PASS " + strRemotePass); if (!(iReplyCode == 230 || iReplyCode == 202)) { CloseSocketConnect();//關閉連接 throw new IOException(strReply.Substring(4)); } } bConnected = true; // 切換到目錄 ChDir(strRemotePath); } /// <summary> /// 關閉連接 /// </summary> public void DisConnect() { if (socketControl != null) { SendCommand("QUIT"); } CloseSocketConnect(); } #endregion #region 傳輸模式 /// <summary> /// 傳輸模式:二進制類型、ASCII類型 /// </summary> public enum TransferType { Binary, ASCII }; /// <summary> /// 設置傳輸模式 /// </summary> /// <param name="ttType">傳輸模式</param> public void SetTransferType(TransferType ttType) { if (ttType == TransferType.Binary) { SendCommand("TYPE I");//binary類型傳輸 } else { SendCommand("TYPE A");//ASCII類型傳輸 } if (iReplyCode != 200) { throw new IOException(strReply.Substring(4)); } else { trType = ttType; } } /// <summary> /// 獲得傳輸模式 /// </summary> /// <returns>傳輸模式</returns> public TransferType GetTransferType() { return trType; } #endregion #region 文件操作 /// <summary> /// 獲得文件列表 /// </summary> /// <param name="strMask">文件名的匹配字符串</param> /// <returns></returns> public string[] Dir(string strMask) { // 建立鏈接 if (!bConnected) { Connect(); } //建立進行數據連接的socket Socket socketData = CreateDataSocket(); //傳送命令 SendCommand("NLST " + strMask); //分析應答</pre>
本文由用戶 bdnp 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!