欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

FTPClientHelper輔助類 實(shí)現(xiàn)文件上傳,目錄操作,下載等操作

 更新時(shí)間:2016年06月23日 11:11:26   作者:無恨星晨  
這篇文章主要分享了一個(gè)FTPClientHelper輔助類和介紹了常用的FTP命令,需要的朋友可以參考下。

文檔說明

  本文檔使用Socket通信方式來實(shí)現(xiàn)ftp文件的上傳下載等命令的執(zhí)行

1.基本介紹

  由于最近的項(xiàng)目是客戶端的程序,需要將客戶端的圖片文件【切圖】-【打包】-【ftp上傳】,現(xiàn)在就差最后一步了,慢慢的把這些小功能實(shí)現(xiàn)了,合并到一起就是一個(gè)大功能了,所以一個(gè)業(yè)務(wù)需要拆分的很小很小才可以看清楚,這個(gè)項(xiàng)目實(shí)際需要用到哪些知識(shí)點(diǎn),下面介紹一下ftp上傳的命令

  ftp命令的參考鏈接:http://www.dbjr.com.cn/article/12199.htm
  ftp適合小文件上傳
  對(duì)帶寬要求要求較高
  服務(wù)器安全性也要考慮到
  命令需要熟悉,不然比較難

2.實(shí)際項(xiàng)目

  文件上傳
  文件下載
  刪除文件
  創(chuàng)建文件夾
  文件夾重命名
  刪除文件夾
  改變目錄
  獲取文件夾中文件列表
  等等

2.1 圖片上傳和下載

http://img.jbzj.com/file_images/article/201606/2016062311075220.png

寫了幾個(gè)方法,一般用的最多的就是Put,具體的可以下載復(fù)制源碼下來進(jìn)行實(shí)戰(zhàn)一下。

2.2 目錄創(chuàng)建和刪除

http://img.jbzj.com/file_images/article/201606/2016062311075221.png

這個(gè)方法今天剛好用上了,折騰了一會(huì),才搞定的。

3.調(diào)用代碼參考

由于這個(gè)幫助類不是靜態(tài)的,所以需要實(shí)例化

string userName = "xxx";
string password = "xxx";
var ftp = new FTPClientHelper("xxx", ".", userName, password, 1021);

下面還是調(diào)用常用的方法,就可以了,因?yàn)橘~號(hào)、密碼、服務(wù)器的IP地址都被我用“xxx”代替了,所以大家自己改下,還有ftp默認(rèn)端口號(hào)是:1021,如果有變動(dòng)還是需要自己改下的。

4.FTPClientHelper下載

//-------------------------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2015 , ZTO , Ltd .
//-------------------------------------------------------------------------------------

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace ZTO.PicTest.Utilities
{
  /// <summary>
  /// FTP操作幫助類
  ///
  /// 修改紀(jì)錄
  ///
  ///     2016-4-4 版本:1.0 YangHengLian 創(chuàng)建主鍵,注意命名空間的排序,測試非常好。
  /// 
  /// 版本:1.0
  ///
  /// <author>
  ///    <name>YangHengLian</name>
  ///    <date>2016-4-4</date>
  /// </author>
  /// </summary>
  public class FTPClientHelper
  {
    public static object Obj = new object();

    #region 構(gòu)造函數(shù)
    /// <summary>
    /// 缺省構(gòu)造函數(shù)
    /// </summary>
    public FTPClientHelper()
    {
      RemoteHost = "";
      _strRemotePath = "";
      _strRemoteUser = "";
      _strRemotePass = "";
      _strRemotePort = 21;
      _bConnected = false;
    }

    /// <summary>
    /// 構(gòu)造函數(shù)
    /// </summary>
    public FTPClientHelper(string remoteHost, string remotePath, string remoteUser, string remotePass, int remotePort)
    {
      // Ip地址
      RemoteHost = remoteHost;
      // 這個(gè)很重要,表示連接路徑,如果是.表示根目錄
      _strRemotePath = remotePath;
      // 登錄賬號(hào)
      _strRemoteUser = remoteUser;
      // 登錄密碼
      _strRemotePass = remotePass;
      // ftp端口號(hào)
      _strRemotePort = remotePort;

      Connect();
    }
    #endregion

    #region 字段
    private int _strRemotePort;
    private Boolean _bConnected;
    private string _strRemotePass;
    private string _strRemoteUser;
    private string _strRemotePath;

    /// <summary>
    /// 服務(wù)器返回的應(yīng)答信息(包含應(yīng)答碼)
    /// </summary>
    private string _strMsg;
    /// <summary>
    /// 服務(wù)器返回的應(yīng)答信息(包含應(yīng)答碼)
    /// </summary>
    private string _strReply;
    /// <summary>
    /// 服務(wù)器返回的應(yīng)答碼
    /// </summary>
    private int _iReplyCode;
    /// <summary>
    /// 進(jìn)行控制連接的socket
    /// </summary>
    private Socket _socketControl;
    /// <summary>
    /// 傳輸模式
    /// </summary>
    private TransferType _trType;

    /// <summary>
    /// 接收和發(fā)送數(shù)據(jù)的緩沖區(qū)
    /// </summary>
    private const int BlockSize = 512;

    /// <summary>
    /// 編碼方式
    /// </summary>
    readonly Encoding _ascii = Encoding.ASCII;
    /// <summary>
    /// 字節(jié)數(shù)組
    /// </summary>
    readonly Byte[] _buffer = new Byte[BlockSize];
    #endregion

    #region 屬性

    /// <summary>
    /// FTP服務(wù)器IP地址
    /// </summary>
    public string RemoteHost { get; set; }

    /// <summary>
    /// FTP服務(wù)器端口
    /// </summary>
    public int RemotePort
    {
      get
      {
        return _strRemotePort;
      }
      set
      {
        _strRemotePort = value;
      }
    }

    /// <summary>
    /// 當(dāng)前服務(wù)器目錄
    /// </summary>
    public string RemotePath
    {
      get
      {
        return _strRemotePath;
      }
      set
      {
        _strRemotePath = value;
      }
    }

    /// <summary>
    /// 登錄用戶賬號(hào)
    /// </summary>
    public string RemoteUser
    {
      set
      {
        _strRemoteUser = value;
      }
    }

    /// <summary>
    /// 用戶登錄密碼
    /// </summary>
    public string RemotePass
    {
      set
      {
        _strRemotePass = value;
      }
    }

    /// <summary>
    /// 是否登錄
    /// </summary>
    public bool Connected
    {
      get
      {
        return _bConnected;
      }
    }
    #endregion

    #region 鏈接
    /// <summary>
    /// 建立連接 
    /// </summary>
    public void Connect()
    {
      lock (Obj)
      {
        _socketControl = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        var ep = new IPEndPoint(IPAddress.Parse(RemoteHost), _strRemotePort);
        try
        {
          _socketControl.Connect(ep);
        }
        catch (Exception)
        {
          throw new IOException("不能連接ftp服務(wù)器");
        }
      }
      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>
    /// 關(guān)閉連接
    /// </summary>
    public void DisConnect()
    {
      if (_socketControl != null)
      {
        SendCommand("QUIT");
      }
      CloseSocketConnect();
    }
    #endregion

    #region 傳輸模式
    /// <summary>
    /// 傳輸模式:二進(jìn)制類型、ASCII類型
    /// </summary>
    public enum TransferType { Binary, ASCII };

    /// <summary>
    /// 設(shè)置傳輸模式
    /// </summary>
    /// <param name="ttType">傳輸模式</param>
    public void SetTransferType(TransferType ttType)
    {
      SendCommand(ttType == TransferType.Binary ? "TYPE I" : "TYPE A");
      if (_iReplyCode != 200)
      {
        throw new IOException(_strReply.Substring(4));
      }
      _trType = ttType;
    }

    /// <summary>
    /// 獲得傳輸模式
    /// </summary>
    /// <returns>傳輸模式</returns>
    public TransferType GetTransferType()
    {
      return _trType;
    }
    #endregion

    #region 文件操作
    /// <summary>
    /// 獲得文件列表
    /// </summary>
    /// <param name="strMask">文件名的匹配字符串</param>
    public string[] Dir(string strMask)
    {
      if (!_bConnected)
      {
        Connect();
      }
      Socket socketData = CreateDataSocket();
      SendCommand("NLST " + strMask);
      if (!(_iReplyCode == 150 || _iReplyCode == 125 || _iReplyCode == 226))
      {
        throw new IOException(_strReply.Substring(4));
      }
      _strMsg = "";
      Thread.Sleep(2000);
      while (true)
      {
        int iBytes = socketData.Receive(_buffer, _buffer.Length, 0);
        _strMsg += _ascii.GetString(_buffer, 0, iBytes);
        if (iBytes < _buffer.Length)
        {
          break;
        }
      }
      char[] seperator = { '\n' };
      string[] strsFileList = _strMsg.Split(seperator);
      socketData.Close(); //數(shù)據(jù)socket關(guān)閉時(shí)也會(huì)有返回碼
      if (_iReplyCode != 226)
      {
        ReadReply();
        if (_iReplyCode != 226)
        {

          throw new IOException(_strReply.Substring(4));
        }
      }
      return strsFileList;
    }

    public void NewPutByGuid(string strFileName, string strGuid)
    {
      if (!_bConnected)
      {
        Connect();
      }
      string str = strFileName.Substring(0, strFileName.LastIndexOf("\\", StringComparison.Ordinal));
      string strTypeName = strFileName.Substring(strFileName.LastIndexOf(".", StringComparison.Ordinal));
      strGuid = str + "\\" + strGuid;
      Socket socketData = CreateDataSocket();
      SendCommand("STOR " + Path.GetFileName(strGuid));
      if (!(_iReplyCode == 125 || _iReplyCode == 150))
      {
        throw new IOException(_strReply.Substring(4));
      }
      var input = new FileStream(strGuid, FileMode.Open);
      input.Flush();
      int iBytes;
      while ((iBytes = input.Read(_buffer, 0, _buffer.Length)) > 0)
      {
        socketData.Send(_buffer, iBytes, 0);
      }
      input.Close();
      if (socketData.Connected)
      {
        socketData.Close();
      }
      if (!(_iReplyCode == 226 || _iReplyCode == 250))
      {
        ReadReply();
        if (!(_iReplyCode == 226 || _iReplyCode == 250))
        {
          throw new IOException(_strReply.Substring(4));
        }
      }
    }

    /// <summary>
    /// 獲取文件大小
    /// </summary>
    /// <param name="strFileName">文件名</param>
    /// <returns>文件大小</returns>
    public long GetFileSize(string strFileName)
    {
      if (!_bConnected)
      {
        Connect();
      }
      SendCommand("SIZE " + Path.GetFileName(strFileName));
      long lSize;
      if (_iReplyCode == 213)
      {
        lSize = Int64.Parse(_strReply.Substring(4));
      }
      else
      {
        throw new IOException(_strReply.Substring(4));
      }
      return lSize;
    }

    /// <summary>
    /// 獲取文件信息
    /// </summary>
    /// <param name="strFileName">文件名</param>
    /// <returns>文件大小</returns>
    public string GetFileInfo(string strFileName)
    {
      if (!_bConnected)
      {
        Connect();
      }
      Socket socketData = CreateDataSocket();
      SendCommand("LIST " + strFileName);
      if (!(_iReplyCode == 150 || _iReplyCode == 125
        || _iReplyCode == 226 || _iReplyCode == 250))
      {
        throw new IOException(_strReply.Substring(4));
      }
      byte[] b = new byte[512];
      MemoryStream ms = new MemoryStream();

      while (true)
      {
        int iBytes = socketData.Receive(b, b.Length, 0);
        ms.Write(b, 0, iBytes);
        if (iBytes <= 0)
        {

          break;
        }
      }
      byte[] bt = ms.GetBuffer();
      string strResult = Encoding.ASCII.GetString(bt);
      ms.Close();
      return strResult;
    }

    /// <summary>
    /// 刪除
    /// </summary>
    /// <param name="strFileName">待刪除文件名</param>
    public void Delete(string strFileName)
    {
      if (!_bConnected)
      {
        Connect();
      }
      SendCommand("DELE " + strFileName);
      if (_iReplyCode != 250)
      {
        throw new IOException(_strReply.Substring(4));
      }
    }

    /// <summary>
    /// 重命名(如果新文件名與已有文件重名,將覆蓋已有文件)
    /// </summary>
    /// <param name="strOldFileName">舊文件名</param>
    /// <param name="strNewFileName">新文件名</param>
    public void Rename(string strOldFileName, string strNewFileName)
    {
      if (!_bConnected)
      {
        Connect();
      }
      SendCommand("RNFR " + strOldFileName);
      if (_iReplyCode != 350)
      {
        throw new IOException(_strReply.Substring(4));
      }
      // 如果新文件名與原有文件重名,將覆蓋原有文件
      SendCommand("RNTO " + strNewFileName);
      if (_iReplyCode != 250)
      {
        throw new IOException(_strReply.Substring(4));
      }
    }
    #endregion

    #region 上傳和下載
    /// <summary>
    /// 下載一批文件
    /// </summary>
    /// <param name="strFileNameMask">文件名的匹配字符串</param>
    /// <param name="strFolder">本地目錄(不得以\結(jié)束)</param>
    public void Get(string strFileNameMask, string strFolder)
    {
      if (!_bConnected)
      {
        Connect();
      }
      string[] strFiles = Dir(strFileNameMask);
      foreach (string strFile in strFiles)
      {
        if (!strFile.Equals(""))//一般來說strFiles的最后一個(gè)元素可能是空字符串
        {
          Get(strFile, strFolder, strFile);
        }
      }
    }

    /// <summary>
    /// 下載一個(gè)文件
    /// </summary>
    /// <param name="strRemoteFileName">要下載的文件名</param>
    /// <param name="strFolder">本地目錄(不得以\結(jié)束)</param>
    /// <param name="strLocalFileName">保存在本地時(shí)的文件名</param>
    public void Get(string strRemoteFileName, string strFolder, string strLocalFileName)
    {
      Socket socketData = CreateDataSocket();
      try
      {
        if (!_bConnected)
        {
          Connect();
        }
        SetTransferType(TransferType.Binary);
        if (strLocalFileName.Equals(""))
        {
          strLocalFileName = strRemoteFileName;
        }
        SendCommand("RETR " + strRemoteFileName);
        if (!(_iReplyCode == 150 || _iReplyCode == 125 || _iReplyCode == 226 || _iReplyCode == 250))
        {
          throw new IOException(_strReply.Substring(4));
        }
        var output = new FileStream(strFolder + "\\" + strLocalFileName, FileMode.Create);
        while (true)
        {
          int iBytes = socketData.Receive(_buffer, _buffer.Length, 0);
          output.Write(_buffer, 0, iBytes);
          if (iBytes <= 0)
          {
            break;
          }
        }
        output.Close();
        if (socketData.Connected)
        {
          socketData.Close();
        }
        if (!(_iReplyCode == 226 || _iReplyCode == 250))
        {
          ReadReply();
          if (!(_iReplyCode == 226 || _iReplyCode == 250))
          {
            throw new IOException(_strReply.Substring(4));
          }
        }
      }
      catch
      {
        socketData.Close();
        _socketControl.Close();
        _bConnected = false;
        _socketControl = null;
      }
    }

    /// <summary>
    /// 下載一個(gè)文件
    /// </summary>
    /// <param name="strRemoteFileName">要下載的文件名</param>
    /// <param name="strFolder">本地目錄(不得以\結(jié)束)</param>
    /// <param name="strLocalFileName">保存在本地時(shí)的文件名</param>
    public void GetNoBinary(string strRemoteFileName, string strFolder, string strLocalFileName)
    {
      if (!_bConnected)
      {
        Connect();
      }

      if (strLocalFileName.Equals(""))
      {
        strLocalFileName = strRemoteFileName;
      }
      Socket socketData = CreateDataSocket();
      SendCommand("RETR " + strRemoteFileName);
      if (!(_iReplyCode == 150 || _iReplyCode == 125 || _iReplyCode == 226 || _iReplyCode == 250))
      {
        throw new IOException(_strReply.Substring(4));
      }
      var output = new FileStream(strFolder + "\\" + strLocalFileName, FileMode.Create);
      while (true)
      {
        int iBytes = socketData.Receive(_buffer, _buffer.Length, 0);
        output.Write(_buffer, 0, iBytes);
        if (iBytes <= 0)
        {
          break;
        }
      }
      output.Close();
      if (socketData.Connected)
      {
        socketData.Close();
      }
      if (!(_iReplyCode == 226 || _iReplyCode == 250))
      {
        ReadReply();
        if (!(_iReplyCode == 226 || _iReplyCode == 250))
        {
          throw new IOException(_strReply.Substring(4));
        }
      }
    }

    /// <summary>
    /// 上傳一批文件
    /// </summary>
    /// <param name="strFolder">本地目錄(不得以\結(jié)束)</param>
    /// <param name="strFileNameMask">文件名匹配字符(可以包含*和?)</param>
    public void Put(string strFolder, string strFileNameMask)
    {
      string[] strFiles = Directory.GetFiles(strFolder, strFileNameMask);
      foreach (string strFile in strFiles)
      {
        Put(strFile);
      }
    }

    /// <summary>
    /// 上傳一個(gè)文件
    /// </summary>
    /// <param name="strFileName">本地文件名</param>
    public void Put(string strFileName)
    {
      if (!_bConnected)
      {
        Connect();
      }
      Socket socketData = CreateDataSocket();
      if (Path.GetExtension(strFileName) == "")
        SendCommand("STOR " + Path.GetFileNameWithoutExtension(strFileName));
      else
        SendCommand("STOR " + Path.GetFileName(strFileName));

      if (!(_iReplyCode == 125 || _iReplyCode == 150))
      {
        throw new IOException(_strReply.Substring(4));
      }

      var input = new FileStream(strFileName, FileMode.Open);
      int iBytes;
      while ((iBytes = input.Read(_buffer, 0, _buffer.Length)) > 0)
      {
        socketData.Send(_buffer, iBytes, 0);
      }
      input.Close();
      if (socketData.Connected)
      {
        socketData.Close();
      }
      if (!(_iReplyCode == 226 || _iReplyCode == 250))
      {
        ReadReply();
        if (!(_iReplyCode == 226 || _iReplyCode == 250))
        {
          throw new IOException(_strReply.Substring(4));
        }
      }
    }

    /// <summary>
    /// 上傳一個(gè)文件
    /// </summary>
    /// <param name="strFileName">本地文件名</param>
    /// 
    /// <param name="strGuid"> </param>
    public void PutByGuid(string strFileName, string strGuid)
    {
      if (!_bConnected)
      {
        Connect();
      }
      string str = strFileName.Substring(0, strFileName.LastIndexOf("\\", StringComparison.Ordinal));
      string strTypeName = strFileName.Substring(strFileName.LastIndexOf(".", System.StringComparison.Ordinal));
      strGuid = str + "\\" + strGuid;
      File.Copy(strFileName, strGuid);
      File.SetAttributes(strGuid, FileAttributes.Normal);
      Socket socketData = CreateDataSocket();
      SendCommand("STOR " + Path.GetFileName(strGuid));
      if (!(_iReplyCode == 125 || _iReplyCode == 150))
      {
        throw new IOException(_strReply.Substring(4));
      }
      var input = new FileStream(strGuid, FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
      int iBytes = 0;
      while ((iBytes = input.Read(_buffer, 0, _buffer.Length)) > 0)
      {
        socketData.Send(_buffer, iBytes, 0);
      }
      input.Close();
      File.Delete(strGuid);
      if (socketData.Connected)
      {
        socketData.Close();
      }
      if (!(_iReplyCode == 226 || _iReplyCode == 250))
      {
        ReadReply();
        if (!(_iReplyCode == 226 || _iReplyCode == 250))
        {
          throw new IOException(_strReply.Substring(4));
        }
      }
    }
    #endregion

    #region 目錄操作
    /// <summary>
    /// 創(chuàng)建目錄
    /// </summary>
    /// <param name="strDirName">目錄名</param>
    public void MkDir(string strDirName)
    {
      if (!_bConnected)
      {
        Connect();
      }
      SendCommand("MKD " + strDirName);
      if (_iReplyCode != 257)
      {
        throw new IOException(_strReply.Substring(4));
      }
    }

    /// <summary>
    /// 刪除目錄
    /// </summary>
    /// <param name="strDirName">目錄名</param>
    public void RmDir(string strDirName)
    {
      if (!_bConnected)
      {
        Connect();
      }
      SendCommand("RMD " + strDirName);
      if (_iReplyCode != 250)
      {
        throw new IOException(_strReply.Substring(4));
      }
    }

    /// <summary>
    /// 改變目錄
    /// </summary>
    /// <param name="strDirName">新的工作目錄名</param>
    public void ChDir(string strDirName)
    {
      if (strDirName.Equals(".") || strDirName.Equals(""))
      {
        return;
      }
      if (!_bConnected)
      {
        Connect();
      }
      SendCommand("CWD " + strDirName);
      if (_iReplyCode != 250)
      {
        throw new IOException(_strReply.Substring(4));
      }
      this._strRemotePath = strDirName;
    }
    #endregion

    #region 內(nèi)部函數(shù)
    /// <summary>
    /// 將一行應(yīng)答字符串記錄在strReply和strMsg,應(yīng)答碼記錄在iReplyCode
    /// </summary>
    private void ReadReply()
    {
      _strMsg = "";
      _strReply = ReadLine();
      _iReplyCode = Int32.Parse(_strReply.Substring(0, 3));
    }

    /// <summary>
    /// 建立進(jìn)行數(shù)據(jù)連接的socket
    /// </summary>
    /// <returns>數(shù)據(jù)連接socket</returns>
    private Socket CreateDataSocket()
    {
      SendCommand("PASV");
      if (_iReplyCode != 227)
      {
        throw new IOException(_strReply.Substring(4));
      }
      int index1 = _strReply.IndexOf('(');
      int index2 = _strReply.IndexOf(')');
      string ipData = _strReply.Substring(index1 + 1, index2 - index1 - 1);
      int[] parts = new int[6];
      int len = ipData.Length;
      int partCount = 0;
      string buf = "";
      for (int i = 0; i < len && partCount <= 6; i++)
      {
        char ch = Char.Parse(ipData.Substring(i, 1));
        if (Char.IsDigit(ch))
          buf += ch;
        else if (ch != ',')
        {
          throw new IOException("Malformed PASV strReply: " + _strReply);
        }
        if (ch == ',' || i + 1 == len)
        {
          try
          {
            parts[partCount++] = Int32.Parse(buf);
            buf = "";
          }
          catch (Exception)
          {
            throw new IOException("Malformed PASV strReply: " + _strReply);
          }
        }
      }
      string ipAddress = parts[0] + "." + parts[1] + "." + parts[2] + "." + parts[3];
      int port = (parts[4] << 8) + parts[5];
      var s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      var ep = new IPEndPoint(IPAddress.Parse(ipAddress), port);
      try
      {
        s.Connect(ep);
      }
      catch (Exception)
      {
        throw new IOException("無法連接ftp服務(wù)器");
      }
      return s;
    }

    /// <summary>
    /// 關(guān)閉socket連接(用于登錄以前)
    /// </summary>
    private void CloseSocketConnect()
    {
      lock (Obj)
      {
        if (_socketControl != null)
        {
          _socketControl.Close();
          _socketControl = null;
        }
        _bConnected = false;
      }
    }

    /// <summary>
    /// 讀取Socket返回的所有字符串
    /// </summary>
    /// <returns>包含應(yīng)答碼的字符串行</returns>
    private string ReadLine()
    {
      lock (Obj)
      {
        while (true)
        {
          int iBytes = _socketControl.Receive(_buffer, _buffer.Length, 0);
          _strMsg += _ascii.GetString(_buffer, 0, iBytes);
          if (iBytes < _buffer.Length)
          {
            break;
          }
        }
      }
      char[] seperator = { '\n' };
      string[] mess = _strMsg.Split(seperator);
      if (_strMsg.Length > 2)
      {
        _strMsg = mess[mess.Length - 2];
      }
      else
      {
        _strMsg = mess[0];
      }
      if (!_strMsg.Substring(3, 1).Equals(" ")) //返回字符串正確的是以應(yīng)答碼(如220開頭,后面接一空格,再接問候字符串)
      {
        return ReadLine();
      }
      return _strMsg;
    }

    /// <summary>
    /// 發(fā)送命令并獲取應(yīng)答碼和最后一行應(yīng)答字符串
    /// </summary>
    /// <param name="strCommand">命令</param>
    public void SendCommand(String strCommand)
    {
      lock (Obj)
      {
        Byte[] cmdBytes = Encoding.ASCII.GetBytes((strCommand + "\r\n").ToCharArray());
        _socketControl.Send(cmdBytes, cmdBytes.Length, 0);
        Thread.Sleep(100);
        ReadReply();
      }
    }
    #endregion
  }
}

5.FTP常用的命令

#region 程序集 System.dll, v4.0.0.0
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.dll
#endregion

using System;

namespace System.Net
{
  // 摘要:
  //   System.Net.WebRequestMethods.Ftp、System.Net.WebRequestMethods.File 和 System.Net.WebRequestMethods.Http
  //   類的容器類。無法繼承此類
  public static class WebRequestMethods
  {

    // 摘要:
    //   表示可用于 FILE 請(qǐng)求的文件協(xié)議方法的類型。無法繼承此類。
    public static class File
    {
      // 摘要:
      //   表示用來從指定的位置檢索文件的 FILE GET 協(xié)議方法。
      public const string DownloadFile = "GET";
      //
      // 摘要:
      //   表示用來將文件復(fù)制到指定位置的 FILE PUT 協(xié)議方法。
      public const string UploadFile = "PUT";
    }

    // 摘要:
    //   表示可與 FTP 請(qǐng)求一起使用的 FTP 協(xié)議方法的類型。無法繼承此類。
    public static class Ftp
    {
      // 摘要:
      //   表示要用于將文件追加到 FTP 服務(wù)器上的現(xiàn)有文件的 FTP APPE 協(xié)議方法。
      public const string AppendFile = "APPE";
      //
      // 摘要:
      //   表示要用于刪除 FTP 服務(wù)器上的文件的 FTP DELE 協(xié)議方法。
      public const string DeleteFile = "DELE";
      //
      // 摘要:
      //   表示要用于從 FTP 服務(wù)器下載文件的 FTP RETR 協(xié)議方法。
      public const string DownloadFile = "RETR";
      //
      // 摘要:
      //   表示要用于從 FTP 服務(wù)器上的文件檢索日期時(shí)間戳的 FTP MDTM 協(xié)議方法。
      public const string GetDateTimestamp = "MDTM";
      //
      // 摘要:
      //   表示要用于檢索 FTP 服務(wù)器上的文件大小的 FTP SIZE 協(xié)議方法。
      public const string GetFileSize = "SIZE";
      //
      // 摘要:
      //   表示獲取 FTP 服務(wù)器上的文件的簡短列表的 FTP NLIST 協(xié)議方法。
      public const string ListDirectory = "NLST";
      //
      // 摘要:
      //   表示獲取 FTP 服務(wù)器上的文件的詳細(xì)列表的 FTP LIST 協(xié)議方法。
      public const string ListDirectoryDetails = "LIST";
      //
      // 摘要:
      //   表示在 FTP 服務(wù)器上創(chuàng)建目錄的 FTP MKD 協(xié)議方法。
      public const string MakeDirectory = "MKD";
      //
      // 摘要:
      //   表示打印當(dāng)前工作目錄的名稱的 FTP PWD 協(xié)議方法。
      public const string PrintWorkingDirectory = "PWD";
      //
      // 摘要:
      //   表示移除目錄的 FTP RMD 協(xié)議方法。
      public const string RemoveDirectory = "RMD";
      //
      // 摘要:
      //   表示重命名目錄的 FTP RENAME 協(xié)議方法。
      public const string Rename = "RENAME";
      //
      // 摘要:
      //   表示將文件上載到 FTP 服務(wù)器的 FTP STOR 協(xié)議方法。
      public const string UploadFile = "STOR";
      //
      // 摘要:
      //   表示將具有唯一名稱的文件上載到 FTP 服務(wù)器的 FTP STOU 協(xié)議方法。
      public const string UploadFileWithUniqueName = "STOU";
    }

    // 摘要:
    //   表示可與 HTTP 請(qǐng)求一起使用的 HTTP 協(xié)議方法的類型。
    public static class Http
    {
      // 摘要:
      //   表示與代理一起使用的 HTTP CONNECT 協(xié)議方法,該代理可以動(dòng)態(tài)切換到隧道,如 SSL 隧道的情況。
      public const string Connect = "CONNECT";
      //
      // 摘要:
      //   表示一個(gè) HTTP GET 協(xié)議方法。
      public const string Get = "GET";
      //
      // 摘要:
      //   表示一個(gè) HTTP HEAD 協(xié)議方法。除了服務(wù)器在響應(yīng)中只返回消息頭不返回消息體以外,HEAD 方法和 GET 是一樣的。
      public const string Head = "HEAD";
      //
      // 摘要:
      //   表示一個(gè) HTTP MKCOL 請(qǐng)求,該請(qǐng)求在請(qǐng)求 URI(統(tǒng)一資源標(biāo)識(shí)符)指定的位置新建集合,如頁的集合。
      public const string MkCol = "MKCOL";
      //
      // 摘要:
      //   表示一個(gè) HTTP POST 協(xié)議方法,該方法用于將新實(shí)體作為補(bǔ)充發(fā)送到某個(gè) URI。
      public const string Post = "POST";
      //
      // 摘要:
      //   表示一個(gè) HTTP PUT 協(xié)議方法,該方法用于替換 URI 標(biāo)識(shí)的實(shí)體。
      public const string Put = "PUT";
    }
  }
}

以上就是本文的全部內(nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Unity TextMeshPro實(shí)現(xiàn)富文本超鏈接默認(rèn)字體追加字體

    Unity TextMeshPro實(shí)現(xiàn)富文本超鏈接默認(rèn)字體追加字體

    這篇文章主要為大家介紹了Unity TextMeshPro實(shí)現(xiàn)富文本超鏈接默認(rèn)字體追加字體示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • C# .NET 中的緩存實(shí)現(xiàn)詳情

    C# .NET 中的緩存實(shí)現(xiàn)詳情

    軟件開發(fā)中最常用的模式之一是 緩存 ,其包括進(jìn)程內(nèi)緩存、持久性進(jìn)程內(nèi)緩存和分布式緩存,本文我們將主要介紹進(jìn)程內(nèi)緩存,需要的朋友可以參考下面文章的具體內(nèi)容
    2021-09-09
  • c# winform異步不卡界面的實(shí)現(xiàn)方法

    c# winform異步不卡界面的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于c# winform異步不卡界面的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • C#/VB.NET實(shí)現(xiàn)在Word中插入或刪除腳注

    C#/VB.NET實(shí)現(xiàn)在Word中插入或刪除腳注

    腳注,是可以附在文章頁面的最底端的,對(duì)某些東西加以說明,印在書頁下端的注文。這篇文章將為您展示如何通過C#/VB.NET代碼,以編程方式在Word中插入或刪除腳注,需要的可以參考一下
    2023-03-03
  • .NET操作NPOI實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出

    .NET操作NPOI實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出

    NPOI是指構(gòu)建在POI 3.x版本之上的一個(gè)程序,NPOI可以在沒有安裝Office的情況下對(duì)Word或Excel文檔進(jìn)行讀寫操作,下面小編為大家介紹了如何操作NPOI實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出,需要的可以參考一下
    2023-09-09
  • C#寫差異文件備份工具的示例

    C#寫差異文件備份工具的示例

    這篇文章主要介紹了C#寫差異文件備份工具的示例,幫助大家利用c#備份,管理文件,感興趣的朋友可以了解下
    2020-10-10
  • C#使用NPOI對(duì)word進(jìn)行讀寫

    C#使用NPOI對(duì)word進(jìn)行讀寫

    這篇文章介紹了C#使用NPOI對(duì)word進(jìn)行讀寫的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • c# 常見文件路徑Api的使用示例

    c# 常見文件路徑Api的使用示例

    c#編程中經(jīng)常有遇到要處理文件路徑的需求,本文分別講述了如何從程序下面的文件和臨時(shí)目錄下的文件去使用路徑api,感興趣的朋友可以了解下
    2021-05-05
  • C#枚舉中的位運(yùn)算權(quán)限分配淺談

    C#枚舉中的位運(yùn)算權(quán)限分配淺談

    本文介紹C#位運(yùn)算的處理方法,第一步, 先建立一個(gè)枚舉表示所有的權(quán)限管理操作,接下來是權(quán)限的運(yùn)算等。
    2013-05-05
  • 詳解如何在C#/.NET Core中使用責(zé)任鏈模式

    詳解如何在C#/.NET Core中使用責(zé)任鏈模式

    這篇文章主要介紹了詳解如何在C#/.NET Core中使用責(zé)任鏈模式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05

最新評(píng)論