程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> ftp下載目錄下所有文件及文件夾內(遞歸)

ftp下載目錄下所有文件及文件夾內(遞歸)

編輯:C#入門知識

[csharp]
/// <summary>  
   /// ftp文件上傳、下載操作類  
   /// </summary>  
   public class FTPHelper 
   { 
 
       /// <summary>  
       /// ftp用戶名,匿名為“”  
       /// </summary>  
       private string ftpUser; 
 
       /// <summary>  
       /// ftp用戶密碼,匿名為“”  
       /// </summary>  
       private string ftpPassWord; 
 
       /// <summary>  
       ///通過用戶名,密碼連接到FTP服務器  
       /// </summary>  
       /// <param name="ftpUser">ftp用戶名,匿名為“”</param>  
       /// <param name="ftpPassWord">ftp登陸密碼,匿名為“”</param>  
       public FTPHelper(string ftpUser, string ftpPassWord) 
       { 
           this.ftpUser = ftpUser; 
           this.ftpPassWord = ftpPassWord; 
       } 
 
       /// <summary>  
       /// 匿名訪問  
       /// </summary>  
       public FTPHelper() 
       { 
           this.ftpUser = ""; 
           this.ftpPassWord = ""; 
       } 
 
       /// <summary>  
       /// 上傳文件到Ftp服務器  
       /// </summary>  
       /// <param name="uri">把上傳的文件保存為ftp服務器文件的uri,如"ftp://192.168.1.104/capture-212.avi"</param>  
       /// <param name="upLoadFile">要上傳的本地的文件路徑,如D:\capture-2.avi</param>  
       public void UpLoadFile(string UpLoadUri, string upLoadFile) 
       { 
           Stream requestStream = null; 
           FileStream fileStream = null; 
           FtpWebResponse uploadResponse = null; 
 
           try 
           { 
               Uri uri = new Uri(UpLoadUri); 
 
               FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(uri); 
               uploadRequest.Method = WebRequestMethods.Ftp.UploadFile; 
 
               uploadRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord); 
 
               requestStream = uploadRequest.GetRequestStream(); 
               fileStream = File.Open(upLoadFile, FileMode.Open); 
 
               byte[] buffer = new byte[1024]; 
               int bytesRead; 
               while (true) 
               { 
                   bytesRead = fileStream.Read(buffer, 0, buffer.Length); 
                   if (bytesRead == 0) 
                       break; 
                   requestStream.Write(buffer, 0, bytesRead); 
               } 
 
               requestStream.Close(); 
 
               uploadResponse = (FtpWebResponse)uploadRequest.GetResponse(); 
 
           } 
           catch (Exception ex) 
           { 
               throw new Exception("上傳文件到ftp服務器出錯,文件名:" + upLoadFile + "異常信息:" + ex.ToString()); 
           } 
           finally 
           { 
               if (uploadResponse != null) 
                   uploadResponse.Close(); 
               if (fileStream != null) 
                   fileStream.Close(); 
               if (requestStream != null) 
                   requestStream.Close(); 
           } 
       } 
 
       /// <summary>  
       /// 從ftp下載文件到本地服務器  
       /// </summary>  
       /// <param name="downloadUrl">要下載的ftp文件路徑,如ftp://192.168.1.104/capture-2.avi</param>  
       /// <param name="saveFileUrl">本地保存文件的路徑,如(@"d:\capture-22.avi"</param>  
       public void DownLoadFile(string downloadUrl, string saveFileUrl) 
       { 
           Stream responseStream = null; 
           FileStream fileStream = null; 
           StreamReader reader = null; 
 
           try 
           { 
               // string downloadUrl = "ftp://192.168.1.104/capture-2.avi";  
 
               FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(downloadUrl); 
               downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile; 
 
               //string ftpUser = "yoyo";  
               //string ftpPassWord = "123456";  
               downloadRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord); 
 
               FtpWebResponse downloadResponse = (FtpWebResponse)downloadRequest.GetResponse(); 
               responseStream = downloadResponse.GetResponseStream(); 
 
               fileStream = File.Create(saveFileUrl); 
               byte[] buffer = new byte[1024]; 
               int bytesRead; 
               while (true) 
               { 
                   bytesRead = responseStream.Read(buffer, 0, buffer.Length); 
                   if (bytesRead == 0) 
                       break; 
                   fileStream.Write(buffer, 0, bytesRead); 
               } 
           } 
           catch (Exception ex) 
           { 
               throw new Exception("從ftp服務器下載文件出錯,文件名:" + downloadUrl + "異常信息:" + ex.ToString()); 
           } 
           finally 
           { 
               if (reader != null) 
               { 
                   reader.Close(); 
               } 
               if (responseStream != null) 
               { 
                   responseStream.Close(); 
               } 
               if (fileStream != null) 
               { 
                   fileStream.Close(); 
               } 
           } 
       } 
 
 
       /// <summary>  
       /// 從FTP下載文件到本地服務器,支持斷點下載  
       /// </summary>  
       /// <param name="ftpUri">ftp文件路徑,如"ftp://localhost/test.txt"</param>  
       /// <param name="saveFile">保存文件的路徑,如C:\\test.txt</param>  
       public void BreakPointDownLoadFile(string ftpUri, string saveFile) 
       { 
           System.IO.FileStream fs = null; 
           System.Net.FtpWebResponse ftpRes = null; 
           System.IO.Stream resStrm = null; 
           try 
           { 
               //下載文件的URI  
               Uri u = new Uri(ftpUri); 
               //設定下載文件的保存路徑  
               string downFile = saveFile; 
 
               //FtpWebRequest的作成  
               System.Net.FtpWebRequest ftpReq = (System.Net.FtpWebRequest) 
                   System.Net.WebRequest.Create(u); 
               //設定用戶名和密碼  
               ftpReq.Credentials = new System.Net.NetworkCredential(ftpUser, ftpPassWord); 
               //MethodにWebRequestMethods.Ftp.DownloadFile("RETR")設定  
               ftpReq.Method = System.Net.WebRequestMethods.Ftp.DownloadFile; 
               //要求終了後關閉連接  
               ftpReq.KeepAlive = false; 
               //使用ASCII方式傳送  
               ftpReq.UseBinary = false; 
               //設定PASSIVE方式無效  
               ftpReq.UsePassive = false; 
 
               //判斷是否繼續下載  
               //繼續寫入下載文件的FileStream  
 
               if (System.IO.File.Exists(downFile)) 
               { 
                   //繼續下載  
                   ftpReq.ContentOffset = (new System.IO.FileInfo(downFile)).Length; 
                   fs = new System.IO.FileStream( 
                      downFile, System.IO.FileMode.Append, System.IO.FileAccess.Write); 
               } 
               else 
               { 
                   //一般下載  
                   fs = new System.IO.FileStream( 
                       downFile, System.IO.FileMode.Create, System.IO.FileAccess.Write); 
               } 
 
               //取得FtpWebResponse  
               ftpRes = (System.Net.FtpWebResponse)ftpReq.GetResponse(); 
               //為了下載文件取得Stream  
               resStrm = ftpRes.GetResponseStream(); 
               //寫入下載的數據  
               byte[] buffer = new byte[1024]; 
               while (true) 
               { 
                   int readSize = resStrm.Read(buffer, 0, buffer.Length); 
                   if (readSize == 0) 
                       break; 
                   fs.Write(buffer, 0, readSize); 
               } 
           } 
           catch (Exception ex) 
           { 
               throw new Exception("從ftp服務器下載文件出錯,文件名:" + ftpUri + "異常信息:" + ex.ToString()); 
           } 
           finally 
           { 
               fs.Close(); 
               resStrm.Close(); 
               ftpRes.Close(); 
           } 
       } 
 
       #region 從FTP上下載整個文件夾,包括文件夾下的文件和文件夾  
 
       /// <summary>  
       /// 列出FTP服務器上面當前目錄的所有文件和目錄  
       /// </summary>  
       /// <param name="ftpUri">FTP目錄</param>  
       /// <returns></returns>  
       public List<FileStruct> ListFilesAndDirectories(string ftpUri) 
       { 
           WebResponse webresp = null; 
           StreamReader ftpFileListReader = null; 
           FtpWebRequest ftpRequest = null; 
           try 
           { 
               ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpUri)); 
               ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; 
               ftpRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord); 
               webresp = ftpRequest.GetResponse(); 
               ftpFileListReader = new StreamReader(webresp.GetResponseStream(), Encoding.Default); 
           } 
           catch (Exception ex) 
           { 
               throw new Exception("獲取文件列表出錯,錯誤信息如下:" + ex.ToString()); 
           } 
           string Datastring = ftpFileListReader.ReadToEnd(); 
           return GetList(Datastring); 
 
       } 
 
       /// <summary>  
       /// 列出FTP目錄下的所有文件  
       /// </summary>  
       /// <param name="ftpUri">FTP目錄</param>  
       /// <returns></returns>  
       public List<FileStruct> ListFiles(string ftpUri) 
       { 
           List<FileStruct> listAll = ListFilesAndDirectories(ftpUri); 
           List<FileStruct> listFile = new List<FileStruct>(); 
           foreach (FileStruct file in listAll) 
           { 
               if (!file.IsDirectory) 
               { 
                   listFile.Add(file); 
               } 
           } 
           return listFile; 
       } 
 
 
       /// <summary>  
       /// 列出FTP目錄下的所有目錄  
       /// </summary>  
       /// <param name="ftpUri">FRTP目錄</param>  
       /// <returns>目錄列表</returns>  
       public List<FileStruct> ListDirectories(string ftpUri) 
       { 
           List<FileStruct> listAll = ListFilesAndDirectories(ftpUri); 
           List<FileStruct> listDirectory = new List<FileStruct>(); 
           foreach (FileStruct file in listAll) 
           { 
               if (file.IsDirectory) 
               { 
                   listDirectory.Add(file); 
               } 
           } 
           return listDirectory; 
       } 
 
       /// <summary>  
       /// 獲得文件和目錄列表  
       /// </summary>  
       /// <param name="datastring">FTP返回的列表字符信息</param>  
       private List<FileStruct> GetList(string datastring) 
       { 
           List<FileStruct> myListArray = new List<FileStruct>(); 
           string[] dataRecords = datastring.Split('\n'); 
           FileListStyle _directoryListStyle = GuessFileListStyle(dataRecords); 
           foreach (string s in dataRecords) 
           { 
               if (_directoryListStyle != FileListStyle.Unknown && s != "") 
               { 
                   FileStruct f = new FileStruct(); 
                   f.Name = ".."; 
                   switch (_directoryListStyle) 
                   { 
                       case FileListStyle.UnixStyle: 
                           f = ParseFileStructFromUnixStyleRecord(s); 
                           break; 
                       case FileListStyle.WindowsStyle: 
                           f = ParseFileStructFromWindowsStyleRecord(s); 
                           break; 
                   } 
                   if (!(f.Name == "." || f.Name == "..")) 
                   { 
                       myListArray.Add(f); 
                   } 
               } 
           } 
           return myListArray; 
       } 
       /// <summary>  
       /// 從Unix格式中返回文件信息  
       /// </summary>  
       /// <param name="Record">文件信息</param>  
       private FileStruct ParseFileStructFromUnixStyleRecord(string Record) 
       { 
           FileStruct f = new FileStruct(); 
           string processstr = Record.Trim(); 
           f.Flags = processstr.Substring(0, 10); 
           f.IsDirectory = (f.Flags[0] == 'd'); 
           processstr = (processstr.Substring(11)).Trim(); 
           _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);   //跳過一部分  
           f.Owner = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0); 
           f.Group = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0); 
           _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);   //跳過一部分  
           string yearOrTime = processstr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[2]; 
           if (yearOrTime.IndexOf(":") >= 0)  //time  
           { 
               processstr = processstr.Replace(yearOrTime, DateTime.Now.Year.ToString()); 
           } 
           f.CreateTime = DateTime.Parse(_cutSubstringFromStringWithTrim(ref processstr, ' ', 8)); 
           f.Name = processstr;   //最後就是名稱  
           return f; 
       } 
 
       /// <summary>  
       /// 從Windows格式中返回文件信息  
       /// </summary>  
       /// <param name="Record">文件信息</param>  
       private FileStruct ParseFileStructFromWindowsStyleRecord(string Record) 
       { 
           FileStruct f = new FileStruct(); 
           string processstr = Record.Trim(); 
           string dateStr = processstr.Substring(0, 8); 
           processstr = (processstr.Substring(8, processstr.Length - 8)).Trim(); 
           string timeStr = processstr.Substring(0, 7); 
           processstr = (processstr.Substring(7, processstr.Length - 7)).Trim(); 
           DateTimeFormatInfo myDTFI = new CultureInfo("en-US", false).DateTimeFormat; 
           myDTFI.ShortTimePattern = "t"; 
           f.CreateTime = DateTime.Parse(dateStr + " " + timeStr, myDTFI); 
           if (processstr.Substring(0, 5) == "<DIR>") 
           { 
               f.IsDirectory = true; 
               processstr = (processstr.Substring(5, processstr.Length - 5)).Trim(); 
           } 
           else 
           { 
               string[] strs = processstr.Split(new char[] { ' ' },2);// StringSplitOptions.RemoveEmptyEntries);   // true);  
               processstr = strs[1]; 
               f.IsDirectory = false; 
           } 
           f.Name = processstr; 
           return f; 
       } 
       /// <summary>  
       /// 按照一定的規則進行字符串截取  
       /// </summary>  
       /// <param name="s">截取的字符串</param>  
       /// <param name="c">查找的字符</param>  
       /// <param name="startIndex">查找的位置</param>  
       private string _cutSubstringFromStringWithTrim(ref string s, char c, int startIndex) 
       { 
           int pos1 = s.IndexOf(c, startIndex); 
           string retString = s.Substring(0, pos1); 
           s = (s.Substring(pos1)).Trim(); 
           return retString; 
       } 
       /// <summary>  
       /// 判斷文件列表的方式Window方式還是Unix方式  
       /// </summary>  
       /// <param name="recordList">文件信息列表</param>  
       private FileListStyle GuessFileListStyle(string[] recordList) 
       { 
           foreach (string s in recordList) 
           { 
               if (s.Length > 10 
                && Regex.IsMatch(s.Substring(0, 10), "(-|d)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)")) 
               { 
                   return FileListStyle.UnixStyle; 
               } 
               else if (s.Length > 8 
                && Regex.IsMatch(s.Substring(0, 8), "[0-9][0-9]-[0-9][0-9]-[0-9][0-9]")) 
               { 
                   return FileListStyle.WindowsStyle; 
               } 
           } 
           return FileListStyle.Unknown; 
       } 
 
       /// <summary>    
       /// 從FTP下載整個文件夾    
       /// </summary>    
       /// <param name="ftpDir">FTP文件夾路徑</param>    
       /// <param name="saveDir">保存的本地文件夾路徑</param>    
       public void DownFtpDir(string ftpDir, string saveDir) 
       { 
           List<FileStruct> files = ListFilesAndDirectories(ftpDir); 
           if (!Directory.Exists(saveDir)) 
           { 
               Directory.CreateDirectory(saveDir); 
           } 
           foreach (FileStruct f in files) 
           { 
               if (f.IsDirectory) //文件夾,遞歸查詢  
               { 
                   DownFtpDir(ftpDir + "/" + f.Name, saveDir + "\\" + f.Name); 
               } 
               else //文件,直接下載  
               { 
                   DownLoadFile(ftpDir + "/" + f.Name, saveDir + "\\" + f.Name); 
               } 
           } 
       } 
 
 
       #endregion  
   } 
 
   #region 文件信息結構  
   public struct FileStruct 
   { 
       public string Flags; 
       public string Owner; 
       public string Group; 
       public bool IsDirectory; 
       public DateTime CreateTime; 
       public string Name; 
   } 
   public enum FileListStyle 
   { 
       UnixStyle, 
       WindowsStyle, 
       Unknown 
   } 
   #endregion 

 /// <summary>
    /// ftp文件上傳、下載操作類
    /// </summary>
    public class FTPHelper
    {

        /// <summary>
        /// ftp用戶名,匿名為“”
        /// </summary>
        private string ftpUser;

        /// <summary>
        /// ftp用戶密碼,匿名為“”
        /// </summary>
        private string ftpPassWord;

        /// <summary>
        ///通過用戶名,密碼連接到FTP服務器
        /// </summary>
        /// <param name="ftpUser">ftp用戶名,匿名為“”</param>
        /// <param name="ftpPassWord">ftp登陸密碼,匿名為“”</param>
        public FTPHelper(string ftpUser, string ftpPassWord)
        {
            this.ftpUser = ftpUser;
            this.ftpPassWord = ftpPassWord;
        }

        /// <summary>
        /// 匿名訪問
        /// </summary>
        public FTPHelper()
        {
            this.ftpUser = "";
            this.ftpPassWord = "";
        }

        /// <summary>
        /// 上傳文件到Ftp服務器
        /// </summary>
        /// <param name="uri">把上傳的文件保存為ftp服務器文件的uri,如"ftp://192.168.1.104/capture-212.avi"</param>
        /// <param name="upLoadFile">要上傳的本地的文件路徑,如D:\capture-2.avi</param>
        public void UpLoadFile(string UpLoadUri, string upLoadFile)
        {
            Stream requestStream = null;
            FileStream fileStream = null;
            FtpWebResponse uploadResponse = null;

            try
            {
                Uri uri = new Uri(UpLoadUri);

                FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(uri);
                uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;

                uploadRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);

                requestStream = uploadRequest.GetRequestStream();
                fileStream = File.Open(upLoadFile, FileMode.Open);

                byte[] buffer = new byte[1024];
                int bytesRead;
                while (true)
                {
                    bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                        break;
                    requestStream.Write(buffer, 0, bytesRead);
                }

                requestStream.Close();

                uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();

            }
            catch (Exception ex)
            {
                throw new Exception("上傳文件到ftp服務器出錯,文件名:" + upLoadFile + "異常信息:" + ex.ToString());
            }
            finally
            {
                if (uploadResponse != null)
                    uploadResponse.Close();
                if (fileStream != null)
                    fileStream.Close();
                if (requestStream != null)
                    requestStream.Close();
            }
        }

        /// <summary>
        /// 從ftp下載文件到本地服務器
        /// </summary>
        /// <param name="downloadUrl">要下載的ftp文件路徑,如ftp://192.168.1.104/capture-2.avi</param>
        /// <param name="saveFileUrl">本地保存文件的路徑,如(@"d:\capture-22.avi"</param>
        public void DownLoadFile(string downloadUrl, string saveFileUrl)
        {
            Stream responseStream = null;
            FileStream fileStream = null;
            StreamReader reader = null;

            try
            {
                // string downloadUrl = "ftp://192.168.1.104/capture-2.avi";

                FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(downloadUrl);
                downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;

                //string ftpUser = "yoyo";
                //string ftpPassWord = "123456";
                downloadRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);

                FtpWebResponse downloadResponse = (FtpWebResponse)downloadRequest.GetResponse();
                responseStream = downloadResponse.GetResponseStream();

                fileStream = File.Create(saveFileUrl);
                byte[] buffer = new byte[1024];
                int bytesRead;
                while (true)
                {
                    bytesRead = responseStream.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                        break;
                    fileStream.Write(buffer, 0, bytesRead);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("從ftp服務器下載文件出錯,文件名:" + downloadUrl + "異常信息:" + ex.ToString());
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (responseStream != null)
                {
                    responseStream.Close();
                }
                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }
        }


        /// <summary>
        /// 從FTP下載文件到本地服務器,支持斷點下載
        /// </summary>
        /// <param name="ftpUri">ftp文件路徑,如"ftp://localhost/test.txt"</param>
        /// <param name="saveFile">保存文件的路徑,如C:\\test.txt</param>
        public void BreakPointDownLoadFile(string ftpUri, string saveFile)
        {
            System.IO.FileStream fs = null;
            System.Net.FtpWebResponse ftpRes = null;
            System.IO.Stream resStrm = null;
            try
            {
                //下載文件的URI
                Uri u = new Uri(ftpUri);
                //設定下載文件的保存路徑
                string downFile = saveFile;

                //FtpWebRequest的作成
                System.Net.FtpWebRequest ftpReq = (System.Net.FtpWebRequest)
                    System.Net.WebRequest.Create(u);
                //設定用戶名和密碼
                ftpReq.Credentials = new System.Net.NetworkCredential(ftpUser, ftpPassWord);
                //MethodにWebRequestMethods.Ftp.DownloadFile("RETR")設定
                ftpReq.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
                //要求終了後關閉連接
                ftpReq.KeepAlive = false;
                //使用ASCII方式傳送
                ftpReq.UseBinary = false;
                //設定PASSIVE方式無效
                ftpReq.UsePassive = false;

                //判斷是否繼續下載
                //繼續寫入下載文件的FileStream

                if (System.IO.File.Exists(downFile))
                {
                    //繼續下載
                    ftpReq.ContentOffset = (new System.IO.FileInfo(downFile)).Length;
                    fs = new System.IO.FileStream(
                       downFile, System.IO.FileMode.Append, System.IO.FileAccess.Write);
                }
                else
                {
                    //一般下載
                    fs = new System.IO.FileStream(
                        downFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                }

                //取得FtpWebResponse
                ftpRes = (System.Net.FtpWebResponse)ftpReq.GetResponse();
                //為了下載文件取得Stream
                resStrm = ftpRes.GetResponseStream();
                //寫入下載的數據
                byte[] buffer = new byte[1024];
                while (true)
                {
                    int readSize = resStrm.Read(buffer, 0, buffer.Length);
                    if (readSize == 0)
                        break;
                    fs.Write(buffer, 0, readSize);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("從ftp服務器下載文件出錯,文件名:" + ftpUri + "異常信息:" + ex.ToString());
            }
            finally
            {
                fs.Close();
                resStrm.Close();
                ftpRes.Close();
            }
        }

        #region 從FTP上下載整個文件夾,包括文件夾下的文件和文件夾

        /// <summary>
        /// 列出FTP服務器上面當前目錄的所有文件和目錄
        /// </summary>
        /// <param name="ftpUri">FTP目錄</param>
        /// <returns></returns>
        public List<FileStruct> ListFilesAndDirectories(string ftpUri)
        {
            WebResponse webresp = null;
            StreamReader ftpFileListReader = null;
            FtpWebRequest ftpRequest = null;
            try
            {
                ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpUri));
                ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                ftpRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);
                webresp = ftpRequest.GetResponse();
                ftpFileListReader = new StreamReader(webresp.GetResponseStream(), Encoding.Default);
            }
            catch (Exception ex)
            {
                throw new Exception("獲取文件列表出錯,錯誤信息如下:" + ex.ToString());
            }
            string Datastring = ftpFileListReader.ReadToEnd();
            return GetList(Datastring);

        }

        /// <summary>
        /// 列出FTP目錄下的所有文件
        /// </summary>
        /// <param name="ftpUri">FTP目錄</param>
        /// <returns></returns>
        public List<FileStruct> ListFiles(string ftpUri)
        {
            List<FileStruct> listAll = ListFilesAndDirectories(ftpUri);
            List<FileStruct> listFile = new List<FileStruct>();
            foreach (FileStruct file in listAll)
            {
                if (!file.IsDirectory)
                {
                    listFile.Add(file);
                }
            }
            return listFile;
        }


        /// <summary>
        /// 列出FTP目錄下的所有目錄
        /// </summary>
        /// <param name="ftpUri">FRTP目錄</param>
        /// <returns>目錄列表</returns>
        public List<FileStruct> ListDirectories(string ftpUri)
        {
            List<FileStruct> listAll = ListFilesAndDirectories(ftpUri);
            List<FileStruct> listDirectory = new List<FileStruct>();
            foreach (FileStruct file in listAll)
            {
                if (file.IsDirectory)
                {
                    listDirectory.Add(file);
                }
            }
            return listDirectory;
        }

        /// <summary>
        /// 獲得文件和目錄列表
        /// </summary>
        /// <param name="datastring">FTP返回的列表字符信息</param>
        private List<FileStruct> GetList(string datastring)
        {
            List<FileStruct> myListArray = new List<FileStruct>();
            string[] dataRecords = datastring.Split('\n');
            FileListStyle _directoryListStyle = GuessFileListStyle(dataRecords);
            foreach (string s in dataRecords)
            {
                if (_directoryListStyle != FileListStyle.Unknown && s != "")
                {
                    FileStruct f = new FileStruct();
                    f.Name = "..";
                    switch (_directoryListStyle)
                    {
                        case FileListStyle.UnixStyle:
                            f = ParseFileStructFromUnixStyleRecord(s);
                            break;
                        case FileListStyle.WindowsStyle:
                            f = ParseFileStructFromWindowsStyleRecord(s);
                            break;
                    }
                    if (!(f.Name == "." || f.Name == ".."))
                    {
                        myListArray.Add(f);
                    }
                }
            }
            return myListArray;
        }
        /// <summary>
        /// 從Unix格式中返回文件信息
        /// </summary>
        /// <param name="Record">文件信息</param>
        private FileStruct ParseFileStructFromUnixStyleRecord(string Record)
        {
            FileStruct f = new FileStruct();
            string processstr = Record.Trim();
            f.Flags = processstr.Substring(0, 10);
            f.IsDirectory = (f.Flags[0] == 'd');
            processstr = (processstr.Substring(11)).Trim();
            _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);   //跳過一部分
            f.Owner = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);
            f.Group = _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);
            _cutSubstringFromStringWithTrim(ref processstr, ' ', 0);   //跳過一部分
            string yearOrTime = processstr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[2];
            if (yearOrTime.IndexOf(":") >= 0)  //time
            {
                processstr = processstr.Replace(yearOrTime, DateTime.Now.Year.ToString());
            }
            f.CreateTime = DateTime.Parse(_cutSubstringFromStringWithTrim(ref processstr, ' ', 8));
            f.Name = processstr;   //最後就是名稱
            return f;
        }

        /// <summary>
        /// 從Windows格式中返回文件信息
        /// </summary>
        /// <param name="Record">文件信息</param>
        private FileStruct ParseFileStructFromWindowsStyleRecord(string Record)
        {
            FileStruct f = new FileStruct();
            string processstr = Record.Trim();
            string dateStr = processstr.Substring(0, 8);
            processstr = (processstr.Substring(8, processstr.Length - 8)).Trim();
            string timeStr = processstr.Substring(0, 7);
            processstr = (processstr.Substring(7, processstr.Length - 7)).Trim();
            DateTimeFormatInfo myDTFI = new CultureInfo("en-US", false).DateTimeFormat;
            myDTFI.ShortTimePattern = "t";
            f.CreateTime = DateTime.Parse(dateStr + " " + timeStr, myDTFI);
            if (processstr.Substring(0, 5) == "<DIR>")
            {
                f.IsDirectory = true;
                processstr = (processstr.Substring(5, processstr.Length - 5)).Trim();
            }
            else
            {
                string[] strs = processstr.Split(new char[] { ' ' },2);// StringSplitOptions.RemoveEmptyEntries);   // true);
                processstr = strs[1];
                f.IsDirectory = false;
            }
            f.Name = processstr;
            return f;
        }
        /// <summary>
        /// 按照一定的規則進行字符串截取
        /// </summary>
        /// <param name="s">截取的字符串</param>
        /// <param name="c">查找的字符</param>
        /// <param name="startIndex">查找的位置</param>
        private string _cutSubstringFromStringWithTrim(ref string s, char c, int startIndex)
        {
            int pos1 = s.IndexOf(c, startIndex);
            string retString = s.Substring(0, pos1);
            s = (s.Substring(pos1)).Trim();
            return retString;
        }
        /// <summary>
        /// 判斷文件列表的方式Window方式還是Unix方式
        /// </summary>
        /// <param name="recordList">文件信息列表</param>
        private FileListStyle GuessFileListStyle(string[] recordList)
        {
            foreach (string s in recordList)
            {
                if (s.Length > 10
                 && Regex.IsMatch(s.Substring(0, 10), "(-|d)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)(-|r)(-|w)(-|x)"))
                {
                    return FileListStyle.UnixStyle;
                }
                else if (s.Length > 8
                 && Regex.IsMatch(s.Substring(0, 8), "[0-9][0-9]-[0-9][0-9]-[0-9][0-9]"))
                {
                    return FileListStyle.WindowsStyle;
                }
            }
            return FileListStyle.Unknown;
        }

        /// <summary> 
        /// 從FTP下載整個文件夾 
        /// </summary> 
        /// <param name="ftpDir">FTP文件夾路徑</param> 
        /// <param name="saveDir">保存的本地文件夾路徑</param> 
        public void DownFtpDir(string ftpDir, string saveDir)
        {
            List<FileStruct> files = ListFilesAndDirectories(ftpDir);
            if (!Directory.Exists(saveDir))
            {
                Directory.CreateDirectory(saveDir);
            }
            foreach (FileStruct f in files)
            {
                if (f.IsDirectory) //文件夾,遞歸查詢
                {
                    DownFtpDir(ftpDir + "/" + f.Name, saveDir + "\\" + f.Name);
                }
                else //文件,直接下載
                {
                    DownLoadFile(ftpDir + "/" + f.Name, saveDir + "\\" + f.Name);
                }
            }
        }


        #endregion
    }

    #region 文件信息結構
    public struct FileStruct
    {
        public string Flags;
        public string Owner;
        public string Group;
        public bool IsDirectory;
        public DateTime CreateTime;
        public string Name;
    }
    public enum FileListStyle
    {
        UnixStyle,
        WindowsStyle,
        Unknown
    }
    #endregion
 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved