.net 開源 FTP 組件 edtFTPnet

edtFTPnet官方網站:
http://www.enterprisedt.com/products/edtftpnet/

目前最新版本爲2.2.3,下載後在bin目錄中找到edtFTPnet.dll,在項目中添加引用。
下面爲一些用法:

一、FTP服務器根目錄爲web,在它下面建立以下圖示的目錄和上傳文件
FTPConnection.CommandEncoding = Encoding.GetEncoding("GBK") 指定GBK編碼以支持中文文件名
FTPConnection.CreateDirectory() 建立指定目錄
FTPConnection.ChangeWorkingDirectory() 切換到指定目錄
FTPConnection.UploadFile() 上傳文件

web
|-----L11
      |-----L21            
            |-----L31
                  |-----31.htm
            |-----21.htm
      |-----L22
      |-----11.htm
      |-----目錄.txtweb

private const string ftpServerIP = "xx.xx.xx.xx" ;
private const string ftpRemotePath = "web" ;
private const string ftpUserID = "user" ;
private const string ftpPassword = "123" ;
 
private void btnCreateAndUpload_Click( object sender, EventArgs e)
{         
     using ( var conn = new FTPConnection
     {
         ServerAddress = ftpServerIP,
         ServerDirectory = ftpRemotePath,
         UserName = ftpUserID,
         Password = ftpPassword,
         CommandEncoding = Encoding.GetEncoding( "GBK" )
     })
     {
         conn.Connect();               
 
         //建立目錄L11
         conn.CreateDirectory( "L11" );
 
         //============切換當前工做目錄到L11============
         conn.ChangeWorkingDirectory( "L11" );
         //上行代碼也能夠用conn.ServerDirectory = "L11";
 
         //上傳"文檔.txt"
         conn.UploadFile( "文檔.txt" , "文檔.txt" );
 
         //建立子目錄L21,L22
         conn.CreateDirectory( "L21" );
         conn.CreateDirectory( "L22" );
 
         //在當前工做目錄L11上傳文件"L11.htm"
         conn.UploadFile( "11.htm" , "11.htm" );
 
         //============切換當前工做目錄到L21============
         conn.ChangeWorkingDirectory( "L21" );
 
         //上傳文件21.htm
         conn.UploadFile( "21.htm" , "21.htm" );
         //建立目錄L31
         conn.CreateDirectory( "L31" );
 
         //============切換當前工做目錄到L31============
         conn.ChangeWorkingDirectory( "L31" );
 
         //上傳文件31.htm
         conn.UploadFile( "31.htm" , "31.htm" );
     }
}

二、獲取文件信息
FTPConnection.GetFileInfos() 獲取文件和目錄詳細信息,結果的一個屬性Dir爲true表示爲目錄,不然爲文件服務器

private void btnList_Click( object sender, EventArgs e)
{
     using ( var conn = new FTPConnection
     {
         ServerAddress = ftpServerIP,
         ServerDirectory = ftpRemotePath,
         UserName = ftpUserID,
         Password = ftpPassword,
         CommandEncoding = Encoding.GetEncoding( "GBK" )
     })
     {
         conn.Connect();
         //獲取指定目錄的全部子目錄和文件信息
         conn.ChangeWorkingDirectory( "L11/L21" );              
         var files1 = conn.GetFileInfos();
         ShowFileInfos(files1);
         /*結果以下:
          File, /web/L11/L21/21.htm, 21.htm, 11.2KB, 2013-9-26 14:51:00
          Dir, /web/L11/L21/L31, L31, 0KB, 2013-9-26 14:51:00
          */
 
         ////返回上級目錄,例子中爲web/L11
         conn.ChangeWorkingDirectoryUp();
         var files2 = conn.GetFileInfosRecursive();
         ShowFileInfos(files2);
         /*結果以下:
         File, /web/L11/11.htm, 11.htm, 59.4KB, 2013-9-26 14:51:00
         Dir, /web/L11/L21, L21, 0KB, 2013-9-26 14:51:00
         Dir, /web/L11/L22, L22, 0KB, 2013-9-26 14:51:00
         File, /web/L11/文檔.txt, 文檔.txt, 40.8KB, 2013-9-26 14:51:00
         File, /web/L11/L21/21.htm, 21.htm, 11.2KB, 2013-9-26 14:51:00
         Dir, /web/L11/L21/L31, L31, 0KB, 2013-9-26 14:51:00
         File, /web/L11/L21/L31/31.htm, 31.htm, 5.6KB, 2013-9-26 14:51:00
          */
     }
}     
void ShowFileInfos(FTPFile[] files)
{
     foreach ( var file in files)
     {
         string info = string .Format( "{0}, {1}, {2}, {3}KB, {4}" ,
             file.Dir ? "Dir" : "File" , file.Path, file.Name, Math.Round(file.Size / 1024.0, 1), file.LastModified);
         richTextBox1.AppendText(info + "\r\n" );
     }
     richTextBox1.AppendText( "\r\n" );
}

其中遞歸獲取指定目錄的全部子目錄和文件信息的擴展方法GetFileInfosRecursive以下:網站

public static class FtpClientExtensions
{
     /// <summary>
     /// 遞歸獲取目錄的全部子目錄和文件信息
     /// </summary>
     public static FTPFile[] GetFileInfosRecursive( this FTPConnection conn)
     {
         var resultList = new List<FTPFile>();
         var fileInfos = conn.GetFileInfos();
         resultList.AddRange(fileInfos);
         foreach ( var fileInfo in fileInfos)
         {
             if (fileInfo.Dir)
             {
                 conn.ServerDirectory = fileInfo.Path;
                 resultList.AddRange(conn.GetFileInfosRecursive());
             }
         }
         return resultList.ToArray();
     }
     /// <summary>
     /// 遞歸刪除目錄(包括全部子目錄和文件信息)
     /// </summary>
     public static void DeleteDirectoryRecursive( this FTPConnection conn, string directoryName)
     {
         conn.ChangeWorkingDirectory(directoryName);
         var fileInfos = conn.GetFileInfos();
         foreach ( var fileInfo in fileInfos)
         {
             if (fileInfo.Dir)
                 conn.DeleteDirectoryRecursive(fileInfo.Name);
             else
                 conn.DeleteFile(fileInfo.Name);
         }
         conn.ChangeWorkingDirectoryUp();
         conn.DeleteDirectory(directoryName);
     }
}

三、刪除目錄及全部子目錄和文件
FTPConnection.DeleteDirectory()當刪除的目錄不爲空時,會出錯,需遞歸刪除,
遞歸刪除目錄擴展方法DeleteDirectoryRecursive見上面類FtpClientExtensionsthis

private void btnDelete_Click( object sender, EventArgs e)
{
     using ( var conn = new FTPConnection
     {
         ServerAddress = ftpServerIP,
         ServerDirectory = ftpRemotePath,
         UserName = ftpUserID,
         Password = ftpPassword,
         CommandEncoding = Encoding.GetEncoding( "GBK" )
     })
     {
         conn.Connect();             
         //遞歸刪除目錄及全部子目錄和文件
         conn.DeleteDirectoryRecursive( "L11" );
         //若是刪除單個文件,路徑爲當前目錄的相對路徑
         //conn.DeleteFile("L11/11.htm");
     }
}
相關文章
相關標籤/搜索