C〓〓〓〓はFTPフォルダ/ダウンロードを遍歴します。

5251 ワード

原文のリンク:http://blog.csdn.net/ou8811/article/details/5295780
 
全体のプログラムは大体2つの部分に分けられます。第一部分は単一ファイルのダウンロードを実現する方法です。 
[c-sharp] view plaincopy

/// <summary>  

///           

 /// </summary>  

/// <param name="adss">         </param>  

/// <param name="ftpadss">     FTP  </param>  

public void download(string adss, string ftpadss)  

{  

    //FileMode             ,            。  

    //FileMode.Create       ,       

    FileStream outputStream = new FileStream(adss, FileMode.Create);  

    FtpWebRequest downRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpadss));  

    //       FTP         

    downRequest.Method = WebRequestMethods.Ftp.DownloadFile;  

    FtpWebResponse response = (FtpWebResponse)downRequest.GetResponse();  

    Stream ftpStream = response.GetResponseStream();  

    long cl = response.ContentLength;  

    int bufferSize = 2048;  

    int readCount;  

    byte[] buffer = new byte[bufferSize];  

    readCount = ftpStream.Read(buffer, 0, bufferSize);  

    while (readCount > 0)  

    {  

        outputStream.Write(buffer, 0, readCount);  

        readCount = ftpStream.Read(buffer, 0, bufferSize);  

    }  

    ftpStream.Close();  

    outputStream.Close();  

    response.Close();  

}  

  
  二つ目の部分はつまり指定されたフォルダ内のすべてを巡回する必要があります。 
  まず、フォルダを巡回してフォルダの下のすべてのファイル情報を取得する方法です。  
[c-sharp] view plaincopy

/// </summary>  

/// <param name="ftpads">FTP    </param>  

/// <param name="name">               </param>  

/// <param name="type">    FTP      </param>  

/// <returns></returns>  

public string[] ftp(string ftpads,string name,string type)  

{  

    WebResponse webresp = null;  

    StreamReader ftpFileListReader = null;  

    FtpWebRequest ftpRequest=null;  

    try  

    {  

         ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpads + name));  

         ftpRequest.Method = type;  

         webresp = ftpRequest.GetResponse();  

         ftpFileListReader = new StreamReader(webresp.GetResponseStream(), Encoding.Default);  

    }  

    catch(Exception ex)  

    {  

        ex.ToString();  

          

    }  

    StringBuilder str = new StringBuilder();  

    string line=ftpFileListReader.ReadLine();  

    while (line != null)  

    {  

        str.Append(line);  

        str.Append("/n");  

        line = ftpFileListReader.ReadLine();  

    }  

    string[] fen = str.ToString().Split('/n');  

    return fen;  

}  

フォルダの中にフォルダがあることを避けられないです。後で私達が再帰的なフォルダを実現する方法です。
[c-sharp] view plaincopy

/// <summary>  

///     KO  

/// </summary>  

/// <param name="ftpads">FTP  </param>  

/// <param name="name">        </param>  

/// <param name="Myads">       </param>  

public void downftp(string ftpads, string name,string Myads)  

{  

    string downloadDir = Myads + name;  

    string ftpdir = ftpads + name;  

    string[] fullname = ftp(ftpads, name, WebRequestMethods.Ftp.ListDirectoryDetails);  

    //            

    if (fullname.Length <= 2)  

    {  

        if (fullname[fullname.Length - 1] == "")  

        {  

            download(downloadDir + "/" + name, ftpads + name + "/" + name);  

        }  

    }  

    else  

    {  

        string[] onlyname = ftp(ftpads, name, WebRequestMethods.Ftp.ListDirectory);  

        if (!Directory.Exists(downloadDir))  

        {  

            Directory.CreateDirectory(downloadDir);  

        }  

        foreach (string names in fullname)  

        {  

            //           <DIR>  

            if (names.Contains("<DIR>"))  

            {  

                string olname = names.Split(new string[] { "<DIR>" },   

                StringSplitOptions.None)[1].Trim();  

                downftp(ftpdir, "//" + olname, downloadDir);  

            }  

            else  

            {  

                foreach (string onlynames in onlyname)  

                {  

                    if (onlynames == "" || onlynames == " " || names == "")  

                    {  

                        break;  

                    }  

                    else  

                    {  

                        if (names.Contains(" " + onlynames))  

                        {  

                            download(downloadDir + "/" + onlynames, ftpads + name + "/" +   

                            onlynames);  

                            break;  

                        }  

                    }  

                }  

            }  

        }  

    }  

      

}  

 
  
Webrequest Methods.Ftp.ListDirectoryDetailsを使ってフォルダの下にあるすべてのコンテンツを取得すると、フォルダがあれば、フォルダの詳細情報の中に<DIR>というマークがあります。これを通じて区別できます。
      また、ファイル名とファイル名を取得する際にWebRequest Methods.Ftp.ListDirectoryを使用して、このコマンドはフォルダの下のすべてのファイルにフォルダの名前を含めることができます。この二つのコマンドによって得られた情報を逐一比較すると、ファイルまたはフォルダ名を決定してdownloadとdownftpメソッドに伝えることができます。