c#如何下载ftp整个文件夹?

解决方案 »

  1.   


    C# codepublic void downloadfloder1(string path, string ftp_path) 
    { try { string[] str = GetFilesDetailList(ftp_path); foreach (string s1 in str) { 
    Form1.TextBox1.Text += s1 + Constants.vbCrLf; 
    } int x = 0; 
    for (x = 0; x <= str.Length - 1; x++) { 
    string s = Conversion.str(x).Substring(Conversion.str(x).LastIndexOf(" ") + 1, Conversion.str(x).Length - Conversion.str(x).LastIndexOf(" ") - 1); 
    if (Conversion.str(x)(0) != "d" & Conversion.str(x)(0) != "-") { 
    continue; 

    if (s == "." | s == "..") { 
    continue; 

    if (Conversion.str(x)(0) == "d") { 
    My.Computer.FileSystem.CreateDirectory(path + "/" + s); 
    downloadfloder1(path + "/" + s, ftp_path + "/" + s); 
    //Form1.TextBox1.Text += ftp_path + "/" + s + vbCrLf 
    System.Threading.Thread.Sleep(200); 

    else { downloadfiles(path, ftp_path + "/" + s); //Form1.TextBox1.Text += ftp_path + "/" + s + vbCrLf 
    System.Threading.Thread.Sleep(200); 


    //reqFTP = Nothing 
    return; 

    catch (Exception ex) { 
    //MessageBox.Show(ex.Message) 
    return; 


    public void downloadfiles(string filePath, string fileName) 

    //上面的代码实现了从ftp服务器下载文件的功能 
    try { string onlyFileName = Path.GetFileName(fileName); string newFileName = filePath + "\\" + onlyFileName; if (File.Exists(newFileName)) { return; 

    string url = "ftp://" + ftpServerIP + "/" + fileName; 
    Connect(url); 
    //连接 
    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 
    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 
    Stream ftpStream = response.GetResponseStream(); 
    long cl = response.ContentLength; 
    int bufferSize = 2048; 
    int readCount = 0; 
    byte[] buffer = new byte[bufferSize]; 
    readCount = ftpStream.Read(buffer, 0, bufferSize); FileStream outputStream = new FileStream(newFileName, FileMode.Create); 
    while (readCount > 0) { outputStream.Write(buffer, 0, readCount); 
    readCount = ftpStream.Read(buffer, 0, bufferSize); 

    ftpStream.Close(); 
    outputStream.Close(); response.Close(); 

    catch (Exception ex) { Form1.TextBox1.Text += "ftp://" + ftpServerIP + "/" + fileName + Constants.vbCrLf + ex.Message + Constants.vbCrLf; } 
    }