最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C# 實(shí)現(xiàn)FTP客戶端的小例子

 更新時(shí)間:2020年07月10日 10:54:07   作者:Alan.hsiang  
這篇文章主要介紹了C# 如何實(shí)現(xiàn)FTP客戶端,文中實(shí)例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下

本文是利用C# 實(shí)現(xiàn)FTP客戶端的小例子,主要實(shí)現(xiàn)上傳,下載,刪除等功能,以供學(xué)習(xí)分享使用。

思路:

  • 通過讀取FTP站點(diǎn)的目錄信息,列出對應(yīng)的文件及文件夾。
  • 雙擊目錄,則顯示子目錄,如果是文件,則點(diǎn)擊右鍵,進(jìn)行下載和刪除操作。
  • 通過讀取本地電腦的目錄,以樹狀結(jié)構(gòu)展示,選擇本地文件,右鍵進(jìn)行上傳操作。

涉及知識點(diǎn):

  • FtpWebRequest【實(shí)現(xiàn)文件傳輸協(xié)議 (FTP) 客戶端】 / FtpWebResponse【封裝文件傳輸協(xié)議 (FTP) 服務(wù)器對請求的響應(yīng)】Ftp的操作主要集中在兩個(gè)類中。
  • FlowLayoutPanel  【流布局面板】表示一個(gè)沿水平或垂直方向動態(tài)排放其內(nèi)容的面板。
  • ContextMenuStrip 【快捷菜單】 主要用于右鍵菜單。
  • 資源文件:Resources 用于存放圖片及其他資源。

效果圖如下

左邊:雙擊文件夾進(jìn)入子目錄,點(diǎn)擊工具欄按鈕‘上級目錄'返回。文件點(diǎn)擊右鍵進(jìn)行操作。

右邊:文件夾則點(diǎn)擊前面+號展開。文件則點(diǎn)擊右鍵進(jìn)行上傳。

核心代碼如下

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FtpClient
{
 public class FtpHelper
 {
  #region 屬性與構(gòu)造函數(shù)

  /// <summary>
  /// IP地址
  /// </summary>
  public string IpAddr { get; set; }

  /// <summary>
  /// 相對路徑
  /// </summary>
  public string RelatePath { get; set; }

  /// <summary>
  /// 端口號
  /// </summary>
  public string Port { get; set; }

  /// <summary>
  /// 用戶名
  /// </summary>
  public string UserName { get; set; }

  /// <summary>
  /// 密碼
  /// </summary>
  public string Password { get; set; }



  public FtpHelper() {

  }

  public FtpHelper(string ipAddr, string port, string userName, string password) {
   this.IpAddr = ipAddr;
   this.Port = port;
   this.UserName = userName;
   this.Password = password;
  }

  #endregion

  #region 方法


  /// <summary>
  /// 下載文件
  /// </summary>
  /// <param name="filePath"></param>
  /// <param name="isOk"></param>
  public void DownLoad(string filePath, out bool isOk) {
   string method = WebRequestMethods.Ftp.DownloadFile;
   var statusCode = FtpStatusCode.DataAlreadyOpen;
   FtpWebResponse response = callFtp(method);
   ReadByBytes(filePath, response, statusCode, out isOk);
  }

  public void UpLoad(string file,out bool isOk)
  {
   isOk = false;
   FileInfo fi = new FileInfo(file);
   FileStream fs = fi.OpenRead();
   long length = fs.Length;
   string uri = string.Format("ftp://{0}:{1}{2}", this.IpAddr, this.Port, this.RelatePath);
   FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
   request.Credentials = new NetworkCredential(UserName, Password);
   request.Method = WebRequestMethods.Ftp.UploadFile;
   request.UseBinary = true;
   request.ContentLength = length;
   request.Timeout = 10 * 1000;
   try
   {
    Stream stream = request.GetRequestStream();

    int BufferLength = 2048; //2K
    byte[] b = new byte[BufferLength];
    int i;
    while ((i = fs.Read(b, 0, BufferLength)) > 0)
    {
     stream.Write(b, 0, i);
    }
    stream.Close();
    stream.Dispose();
    isOk = true;
   }
   catch (Exception ex)
   {
    Console.WriteLine(ex.ToString());
   }
   finally {
    if (request != null)
    {
     request.Abort();
     request = null;
    }
   }
  }

  /// <summary>
  /// 刪除文件
  /// </summary>
  /// <param name="isOk"></param>
  /// <returns></returns>
  public string[] DeleteFile(out bool isOk) {
   string method = WebRequestMethods.Ftp.DeleteFile;
   var statusCode = FtpStatusCode.FileActionOK;
   FtpWebResponse response = callFtp(method);
   return ReadByLine(response, statusCode, out isOk);
  }

  /// <summary>
  /// 展示目錄
  /// </summary>
  public string[] ListDirectory(out bool isOk)
  {
   string method = WebRequestMethods.Ftp.ListDirectoryDetails;
   var statusCode = FtpStatusCode.DataAlreadyOpen;
   FtpWebResponse response= callFtp(method);
   return ReadByLine(response, statusCode, out isOk);
  }

  /// <summary>
  /// 設(shè)置上級目錄
  /// </summary>
  public void SetPrePath()
  {
   string relatePath = this.RelatePath;
   if (string.IsNullOrEmpty(relatePath) || relatePath.LastIndexOf("/") == 0 )
   {
    relatePath = "";
   }
   else {
    relatePath = relatePath.Substring(0, relatePath.LastIndexOf("/"));
   }
   this.RelatePath = relatePath;
  }

  #endregion

  #region 私有方法

  /// <summary>
  /// 調(diào)用Ftp,將命令發(fā)往Ftp并返回信息
  /// </summary>
  /// <param name="method">要發(fā)往Ftp的命令</param>
  /// <returns></returns>
  private FtpWebResponse callFtp(string method)
  {
   string uri = string.Format("ftp://{0}:{1}{2}", this.IpAddr, this.Port, this.RelatePath);
   FtpWebRequest request; request = (FtpWebRequest)FtpWebRequest.Create(uri);
   request.UseBinary = true;
   request.UsePassive = true;
   request.Credentials = new NetworkCredential(UserName, Password);
   request.KeepAlive = false;
   request.Method = method;
   FtpWebResponse response = (FtpWebResponse)request.GetResponse();
   return response;
  }

  /// <summary>
  /// 按行讀取
  /// </summary>
  /// <param name="response"></param>
  /// <param name="statusCode"></param>
  /// <param name="isOk"></param>
  /// <returns></returns>
  private string[] ReadByLine(FtpWebResponse response, FtpStatusCode statusCode,out bool isOk) {
   List<string> lstAccpet = new List<string>();
   int i = 0;
   while (true)
   {
    if (response.StatusCode == statusCode)
    {
     using (StreamReader sr = new StreamReader(response.GetResponseStream()))
     {
      string line = sr.ReadLine();
      while (!string.IsNullOrEmpty(line))
      {
       lstAccpet.Add(line);
       line = sr.ReadLine();
      }
     }
     isOk = true;
     break;
    }
    i++;
    if (i > 10)
    {
     isOk = false;
     break;
    }
    Thread.Sleep(200);
   }
   response.Close();
   return lstAccpet.ToArray();
  }

  private void ReadByBytes(string filePath,FtpWebResponse response, FtpStatusCode statusCode, out bool isOk)
  {
   isOk = false;
   int i = 0;
   while (true)

   {
    if (response.StatusCode == statusCode)
    {
     long length = response.ContentLength;
     int bufferSize = 2048;
     int readCount;
     byte[] buffer = new byte[bufferSize];
     using (FileStream outputStream = new FileStream(filePath, FileMode.Create))
     {

      using (Stream ftpStream = response.GetResponseStream())
      {
       readCount = ftpStream.Read(buffer, 0, bufferSize);
       while (readCount > 0)
       {
        outputStream.Write(buffer, 0, readCount);
        readCount = ftpStream.Read(buffer, 0, bufferSize);
       }
      }
     }
     break;
    }
    i++;
    if (i > 10)
    {
     isOk = false;
     break;
    }
    Thread.Sleep(200);
   }
   response.Close();
  }
  #endregion
 }

 /// <summary>
 /// Ftp內(nèi)容類型枚舉
 /// </summary>
 public enum FtpContentType
 {
  undefined = 0,
  file = 1,
  folder = 2
 }
}

FTP服務(wù)端和客戶端示意圖

以上就是C# 實(shí)現(xiàn)FTP客戶端的小例子的詳細(xì)內(nèi)容,更多關(guān)于C# 實(shí)現(xiàn)FTP客戶端的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C# 遞歸查找樹狀目錄實(shí)現(xiàn)方法

    C# 遞歸查找樹狀目錄實(shí)現(xiàn)方法

    這篇文章主要介紹了C# 遞歸查找樹狀目錄實(shí)現(xiàn)方法,需要的朋友可以參考下
    2014-02-02
  • C#比較日期的方法總結(jié)

    C#比較日期的方法總結(jié)

    在本篇內(nèi)容中小編給大家整理了關(guān)于C#比較日期的方法和相關(guān)知識點(diǎn),有需要的朋友們學(xué)習(xí)下。
    2019-02-02
  • C#學(xué)習(xí)筆記整理-迭代器模式介紹

    C#學(xué)習(xí)筆記整理-迭代器模式介紹

    下面小編就為大家分享一篇C#學(xué)習(xí)筆記整理-迭代器模式介紹,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • C#中實(shí)現(xiàn)任意List的全組合算法代碼

    C#中實(shí)現(xiàn)任意List的全組合算法代碼

    這篇文章主要是介紹了.net C# 實(shí)現(xiàn)任意List的全組合算法實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2013-05-05
  • C# Winform 禁止用戶調(diào)整ListView的列寬

    C# Winform 禁止用戶調(diào)整ListView的列寬

    在使用 ListView 的時(shí)候, 有時(shí)我們不想讓別人隨意調(diào)整列寬, 或者某幾列的列寬, 以便達(dá)到美觀, 或者隱藏?cái)?shù)據(jù)的作用. 那么可以用一下代碼來實(shí)現(xiàn)
    2011-05-05
  • 利用C#/VB.NET實(shí)現(xiàn)PPT轉(zhuǎn)換為HTML

    利用C#/VB.NET實(shí)現(xiàn)PPT轉(zhuǎn)換為HTML

    利用PowerPoint可以很方便的呈現(xiàn)多媒體信息,且信息形式多媒體化,表現(xiàn)力強(qiáng)。但難免在某些情況下我們會需要將PowerPoint轉(zhuǎn)換為HTML格式,本文就為大家整理了轉(zhuǎn)換方法,希望對大家有所幫助
    2023-05-05
  • Unity實(shí)現(xiàn)虛擬鍵盤

    Unity實(shí)現(xiàn)虛擬鍵盤

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)虛擬鍵盤,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • C#調(diào)用Nero SDK刻錄光盤的方法

    C#調(diào)用Nero SDK刻錄光盤的方法

    這篇文章主要介紹了C#調(diào)用Nero SDK刻錄光盤的方法,涉及C#調(diào)用NeroCOM組件實(shí)現(xiàn)光盤刻錄的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • C#實(shí)現(xiàn)將像素轉(zhuǎn)換為頁面單位的方法

    C#實(shí)現(xiàn)將像素轉(zhuǎn)換為頁面單位的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)將像素轉(zhuǎn)換為頁面單位的方法,涉及C#像素轉(zhuǎn)換在圖形繪制中的技巧,需要的朋友可以參考下
    2015-06-06
  • C#自定義IP輸入框控件

    C#自定義IP輸入框控件

    這篇文章主要為大家詳細(xì)介紹了C#自定義IP輸入框控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05

最新評論

兴义市| 内丘县| 金阳县| 兴海县| 元氏县| 宜黄县| 澄城县| 马边| 灯塔市| 呼伦贝尔市| 祁连县| 南安市| 丰城市| 仙游县| 南昌市| 冷水江市| 南江县| 福建省| 通州区| 巨野县| 隆尧县| 新龙县| 抚州市| 万盛区| 饶河县| 双桥区| 郁南县| 抚松县| 商南县| 额敏县| 武清区| 万全县| 康定县| 松滋市| 鹤壁市| 孙吴县| 综艺| 大城县| 沙河市| 丹寨县| 蒙阴县|