C#:FTP工具類edtFTPnet使用筆記

最近須要重寫一下一個C#客戶端程序對FTP服務的支持,上網查了一些資料,看到了一個工具類edtFTPnet,因而今天下載了一個包瞭解了下。html

網站首頁、文檔地址、下載地址以下:api

一、網站首頁:http://enterprisedt.com/products/edtftpnet/服務器

二、文檔地址:http://www.enterprisedt.com/products/edtftpnet/doc/manual/index.htmlsession

三、下載地址:http://enterprisedt.com/products/edtftpnet/editions/socket

在下載地址可下載三個版本,功能差別以下圖所示,這裏我選擇Free版工具

下載到本地的包是一個zip壓縮包,解壓縮後目錄結構以下:測試

E:\>tree edtftpnet-2.2.3
文件夾 PATH 列表
卷序列號爲 002E0032 0E91:05C2
E:\EDTFTPNET-2.2.3
├─bin
├─doc
│  ├─images
│  └─manual
│      ├─api
│      │  ├─api
│      │  │  ├─fti
│      │  │  ├─html
│      │  │  ├─icons
│      │  │  ├─scripts
│      │  │  └─styles
│      │  │      └─Whidbey
│      │  ├─fti
│      │  ├─html
│      │  ├─icons
│      │  ├─scripts
│      │  └─styles
│      │      └─Whidbey
│      ├─html
│      ├─images
│      └─rfc
├─examples
│  ├─FTPClientCS
│  ├─FTPClientVB
│  ├─FTPConnectionCS
│  ├─FTPConnectionVB
│  └─Tutorial
│      └─images
├─lib
├─src
│  ├─config
│  ├─net
│  │  └─ftp
│  │      └─test
│  └─util
│      └─debug
└─test
    ├─conf
    ├─data
    └─log

其中bin目錄下文件以下:網站

E:\edtftpnet-2.2.3>tree /f bin
文件夾 PATH 列表
卷序列號爲 002E0032 0E91:05C2
E:\EDTFTPNET-2.2.3\BIN
    edtFTPnet.dll
    test-edtFTPnet.dll

沒有子文件夾

在.NET工程中手工添加引用edtFTPnet.dll,就可使用edtFTPnet相關工具類了。ui

創建、關閉FTP鏈接代碼以下:編碼

FTPConnection ftp = new FTPConnection();
ftp.ServerAddress = "myservername";
ftp.UserName = "myusername";
ftp.Password = "mypassword";
ftp.Connect();
ftp.Close();

上傳下載可採用下面三種方式進行:

Files (DownloadFile(String, String) and UploadFile(String, String)
Streams (DownloadStream(Stream, String) and UploadStream(Stream, String))
Byte-arrays (DownloadByteArray(String) and UploadByteArray(Byte[], String)

重命名、刪除文件、獲取文件大小:

Renaming files using the RenameFile(String, String) method.
Deleting files using the DeleteFile(String) method.
Getting a files size using the GetSize(String) method and its modification time using the GetLastWriteTime(String) method.

服務爲每一個會話維護了一個工做路徑(working directory),當前工做路徑可用WorkingDirectory屬性設置。使用ChangeWorkingDirectory(String)方法可切換工做路徑,使用ChangeWorkingDirectoryUp()方法可切換至父目錄,使用DeleteDirectory(String)可刪除目錄,執行此方法前應保證目錄中全部文件都被刪除

---------------------------------------

下面是我寫的一個DEMO程序。

個人操做系統版本爲Win7旗艦版,.NET版本爲4.5,VS版本爲2012,服務端ServU版本爲10.3.0.1。

第一步,使用ServU工具創建一個FTP服務,在地址192.168.13.98的計算機上創建一個名爲flora的用戶,密碼爲123456。

爲測試代碼方便,開通此用戶的全部權限。

第二步,創建一個C#命令行應用程序,手動添加引用edtFTPnet.dll

第三步,輸入C#代碼以下:

using EnterpriseDT.Net.Ftp;
using EnterpriseDT.Util.Debug;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyFtpDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //日誌設置
            FTPConnection.LogLevel = LogLevel.All;
            FTPConnection.LogToConsole = false;
            FTPConnection.LogToTrace = true;
            FTPConnection.LogFile = "myftpdemo_log_20161017.log";

            //建立FTP鏈接類
            using (var ftpConn = new FTPConnection
                {
                    //鏈接地址、端口號(默認爲21)、用戶名、密碼
                    ServerAddress = "192.168.13.98",
                    ServerPort = 21,
                    UserName = "flora",
                    Password = "123456",

                    //編碼方式必定要和服務器端保持一致,不然後面獲取的文件名可能爲亂碼
                    ConnectMode = FTPConnectMode.PASV,
                    TransferType = FTPTransferType.ASCII,
                    DataEncoding = Encoding.GetEncoding("UTF-8"),
                    CommandEncoding = Encoding.GetEncoding("UTF-8")
                })
            {


                //設置下載先後觸發事件
                ftpConn.TransferNotifyInterval = 1000;
                ftpConn.Downloading += (sender, e) =>
                {
                    Console.WriteLine("XXXXXXXX 下載開始 XXXXXXXX");
                };
                ftpConn.Downloaded += (sender, e) =>
                {
                    Console.WriteLine("XXXXXXXX 下載完畢 XXXXXXXX");
                };

                try
                {
                    //創建FTP鏈接
                    ftpConn.Connect();
                    Console.WriteLine("鏈接創建成功");

                    //獲取文件列表
                    FTPFile[] ftpFiles = ftpConn.GetFileInfos();
                    foreach (FTPFile file in ftpFiles)
                    {
                        Console.WriteLine("---------");
                        Console.WriteLine("文件名:" + file.Name);
                        Console.WriteLine("是否爲目錄:" + file.Dir);
                        Console.WriteLine("文件大小:" + file.Size);
                        Console.WriteLine("最後修改時間:" + file.LastModified.ToString());
                        Console.WriteLine("---------");
                    }

                    //建立/刪除目錄
                    string tempDirectoryName = "TEMP-DIRECTORY";
                    if (ftpConn.DirectoryExists(tempDirectoryName))
                    {
                        ftpConn.DeleteDirectory(tempDirectoryName);
                        Console.WriteLine("已刪除目錄:" + tempDirectoryName);
                    }
                    ftpConn.CreateDirectory(tempDirectoryName);
                    Console.WriteLine("已建立目錄:" + tempDirectoryName);

                    //建立/刪除文件
                    string tempFileName = "TEMP-FILE.tmp";
                    if (ftpConn.Exists(tempFileName))
                    {
                        ftpConn.DeleteFile(tempFileName);
                        Console.WriteLine("已刪除文件:" + tempFileName);
                    }

                    //上傳文件
                    ftpConn.UploadFile("C:\\Users\\Tsybius\\Desktop\\haha.txt", "haha.txt");
                    Console.WriteLine("文件上傳成功:" + "haha.txt");

                    //下載文件
                    if (ftpConn.Exists("haha.txt"))
                    {
                        ftpConn.DownloadFile("C:\\Users\\Tsybius\\Desktop\\hehe.txt", "haha.txt");
                        ftpConn.DeleteFile("haha.txt");
                        Console.WriteLine("已下載並刪除文件:" + "haha.txt");
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    //關閉FTP鏈接
                    ftpConn.Close();
                }
            }

            Console.WriteLine("程序執行完畢");
            Console.Read();
        }
    }
}

代碼包含了以下幾個功能:

  • 設置日誌(支持輸出到文件、輸出到控制檯、輸出到.NET Trace)
  • 創建鏈接(需提供IP地址、用戶名、密碼)
  • 編碼設置
  • 編寫下載開始、完成時觸發事件
  • 建立、刪除指定目錄
  • 建立、刪除指定文件
  • 上傳文件到指定位置
  • 從指定位置下載文件

須要注意的內容有:

  • 編碼設置必定要和服務器端保持一致,不然下載下來的文件頗有多是亂碼

第四步,運行程序,運行結果以下:

程序記錄日誌(myftpdemo_log_20161017.log)以下:

INFO [FTPConnection] 17 十月 2016 17:40:30.891 : OS: 6.1.7601.65536, CLR: 4.0.30319.18444, DLL: 2.2.3.0
INFO [FTPConnection] 17 十月 2016 17:40:30.966 : Built: 11-Feb-2013 15:33:49 EST
INFO [FTPConnection] 17 十月 2016 17:40:30.990 : OS: 6.1.7601.65536, CLR: 4.0.30319.18444, DLL: 2.2.3.0
INFO [FTPConnection] 17 十月 2016 17:40:31.021 : Built: 11-Feb-2013 15:33:49 EST
DEBUG [FTPConnection] 17 十月 2016 17:40:31.057 : Set LocalDirectory='D:\MyPrograms\MyFtpDemo\MyFtpDemo\bin\Debug'
DEBUG [FTPClient] 17 十月 2016 17:40:31.087 : Connecting to 192.168.13.98:21
DEBUG [HostNameResolver] 17 十月 2016 17:40:31.173 : Resolving 192.168.13.98
DEBUG [HostNameResolver] 17 十月 2016 17:40:31.199 : 192.168.13.98 resolved to 192.168.13.98
INFO [BaseSocket] 17 十月 2016 17:40:31.244 : Connecting to 192.168.13.98:21 with timeout 120000 ms
DEBUG [BaseSocket] 17 十月 2016 17:40:31.385 : Successfully connected to 192.168.13.98:21
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.412 : Setting socket timeout=120000
INFO [FTPControlSocket] 17 十月 2016 17:40:31.462 : Command encoding=System.Text.UTF8Encoding
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.491 : StrictReturnCodes=False
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.543 : 220 Serv-U FTP Server v10.3 ready...
DEBUG [FTPConnection] 17 十月 2016 17:40:31.583 : Connected to 192.168.13.98 (instance=0)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.612 : ---> USER flora
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.645 : 331 User name okay, need password.
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.669 : ---> PASS ********
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.704 : 230 User logged in, proceed.
DEBUG [FTPConnection] 17 十月 2016 17:40:31.730 : Successfully logged in
INFO [FTPConnection] 17 十月 2016 17:40:31.767 : Auto FEAT disabled
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.792 : ---> TYPE A
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.821 : 200 Type set to A.
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.847 : ---> PWD
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.875 : 257 "/" is current directory.
DEBUG [FTPConnection] 17 十月 2016 17:40:31.902 : GetFileInfos('')
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.931 : ---> SYST
DEBUG [FTPControlSocket] 17 十月 2016 17:40:31.956 : 215 UNIX Type: L8
DEBUG [FTPFileFactory] 17 十月 2016 17:40:31.978 : Selected UNIX parser
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.002 : ---> PWD
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.029 : 257 "/" is current directory.
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.059 : ---> PASV
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.083 : 227 Entering Passive Mode (192,168,13,98,28,95)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.108 : Server supplied address=192.168.13.98
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.132 : Server supplied port=7263
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.150 : autoPassiveIPSubstitution=True
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.174 : remoteAddr=192.168.13.98
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.204 : Substituting server supplied IP (192.168.13.98) with remote host IP (192.168.13.98)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.229 : NewPassiveDataSocket(192.168.13.98,7263)
INFO [BaseSocket] 17 十月 2016 17:40:32.271 : Connecting to 192.168.13.98:7263 with timeout 120000 ms
DEBUG [BaseSocket] 17 十月 2016 17:40:32.302 : Successfully connected to 192.168.13.98:7263
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.336 : Connected
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.360 : ---> LIST
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.393 : 150 Opening ASCII mode data connection for /bin/ls.
DEBUG [FTPClient] 17 十月 2016 17:40:32.415 : -->drwxrwxrwx   1 user     group           0 Oct 17 17:31 TEMP-DIRECTORY
DEBUG [FTPClient] 17 十月 2016 17:40:32.437 : -->drwxrwxrwx   1 user     group           0 Oct 17 17:23 測試目錄
DEBUG [FTPClient] 17 十月 2016 17:40:32.464 : -->-rw-rw-rw-   1 user     group          12 Oct 17 17:22 測試文件1.txt
DEBUG [FTPClient] 17 十月 2016 17:40:32.490 : -->-rw-rw-rw-   1 user     group       22187 Oct 17 17:23 測試文件2.png
DEBUG [FTPClient] 17 十月 2016 17:40:32.516 : -->-rw-rw-rw-   1 user     group        9741 Oct 17 17:23 測試文件3.xlsx
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.642 : 226 Transfer complete. 363 bytes transferred. 0.35 KB/sec.
DEBUG [FTPClient] 17 十月 2016 17:40:32.668 : Found 5 listing lines
DEBUG [FTPFileFactory] 17 十月 2016 17:40:32.712 : Parse() called using culture: Invariant Language (Invariant Country)
DEBUG [FTPFileFactory] 17 十月 2016 17:40:32.739 : Confirmed format UNIX
DEBUG [FTPConnection] 17 十月 2016 17:40:32.817 : DirectoryExists(TEMP-DIRECTORY)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.843 : ---> CWD TEMP-DIRECTORY
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.871 : 250 Directory changed to /TEMP-DIRECTORY
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.899 : ---> CWD /
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.929 : 250 Directory changed to /
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.950 : ---> RMD TEMP-DIRECTORY
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.977 : 250 RMD command successful.
DEBUG [FTPControlSocket] 17 十月 2016 17:40:32.999 : ---> MKD TEMP-DIRECTORY
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.028 : 257 "/TEMP-DIRECTORY" directory created.
DEBUG [FTPConnection] 17 十月 2016 17:40:33.057 : Exists(TEMP-FILE.tmp)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.080 : ---> SIZE TEMP-FILE.tmp
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.108 : 550 /TEMP-FILE.tmp: No such file.
DEBUG [FTPConnection] 17 十月 2016 17:40:33.132 : UploadFile(C:\Users\Tsybius\Desktop\haha.txt,haha.txt,False)
DEBUG [FTPConnection] 17 十月 2016 17:40:33.156 : Cancel resume
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.180 : ---> REST 0
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.220 : 350 Restarting at 0. Send STORE or RETRIEVE.
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.251 : ---> PASV
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.289 : 227 Entering Passive Mode (192,168,13,98,28,97)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.311 : Server supplied address=192.168.13.98
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.336 : Server supplied port=7265
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.367 : autoPassiveIPSubstitution=True
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.390 : remoteAddr=192.168.13.98
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.423 : Substituting server supplied IP (192.168.13.98) with remote host IP (192.168.13.98)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.448 : NewPassiveDataSocket(192.168.13.98,7265)
INFO [BaseSocket] 17 十月 2016 17:40:33.469 : Connecting to 192.168.13.98:7265 with timeout 120000 ms
DEBUG [BaseSocket] 17 十月 2016 17:40:33.504 : Successfully connected to 192.168.13.98:7265
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.525 : Connected
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.544 : ---> STOR haha.txt
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.580 : 150 Opening ASCII mode data connection for haha.txt.
DEBUG [FTPClient] 17 十月 2016 17:40:33.616 : Closing source stream
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.840 : 226 Transfer complete. 1,198 bytes transferred. 15.00 KB/sec.
DEBUG [FTPConnection] 17 十月 2016 17:40:33.864 : Exists(haha.txt)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.884 : ---> SIZE haha.txt
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.916 : 213 1198
DEBUG [FTPConnection] 17 十月 2016 17:40:33.945 : DownloadFile(C:\Users\Tsybius\Desktop\hehe.txt,haha.txt)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:33.967 : ---> SIZE haha.txt
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.004 : 213 1198
DEBUG [FTPConnection] 17 十月 2016 17:40:34.022 : GetLastWriteTime(haha.txt)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.043 : ---> MDTM haha.txt
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.071 : 213 20161017094033.319
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.151 : ---> REST 0
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.176 : 350 Restarting at 0. Send STORE or RETRIEVE.
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.207 : ---> PASV
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.240 : 227 Entering Passive Mode (192,168,13,98,28,100)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.265 : Server supplied address=192.168.13.98
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.299 : Server supplied port=7268
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.333 : autoPassiveIPSubstitution=True
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.356 : remoteAddr=192.168.13.98
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.375 : Substituting server supplied IP (192.168.13.98) with remote host IP (192.168.13.98)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.393 : NewPassiveDataSocket(192.168.13.98,7268)
INFO [BaseSocket] 17 十月 2016 17:40:34.420 : Connecting to 192.168.13.98:7268 with timeout 120000 ms
DEBUG [BaseSocket] 17 十月 2016 17:40:34.453 : Successfully connected to 192.168.13.98:7268
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.477 : Connected
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.509 : ---> RETR haha.txt
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.568 : 150 Opening ASCII mode data connection for haha.txt (1198 Bytes).
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.841 : 226 Transfer complete. 1,198 bytes transferred. 1.17 KB/sec.
DEBUG [FTPConnection] 17 十月 2016 17:40:34.879 : DeleteFile(haha.txt)
DEBUG [FTPConnection] 17 十月 2016 17:40:34.908 : GetLastWriteTime(haha.txt)
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.937 : ---> MDTM haha.txt
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.963 : 213 20161017094033.319
DEBUG [FTPControlSocket] 17 十月 2016 17:40:34.985 : ---> DELE haha.txt
DEBUG [FTPControlSocket] 17 十月 2016 17:40:35.015 : 250 DELE command successful.
DEBUG [FTPConnection] 17 十月 2016 17:40:35.039 : Closing connection (instance=0)
DEBUG [FTPFileFactory] 17 十月 2016 17:40:35.066 : Defaulting to Unix parsing
DEBUG [FTPControlSocket] 17 十月 2016 17:40:35.093 : ---> QUIT
DEBUG [FTPControlSocket] 17 十月 2016 17:40:35.123 : 221 Goodbye, closing session.

END

相關文章
相關標籤/搜索