从FTP服务器下载文件 [英] Download Files From FTP Server

查看:85
本文介绍了从FTP服务器下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过C#连接FTP服务器并从FTP站点下载文件

How to connect FTP Server Through C# and download the files from FTP site

推荐答案

以下是您需要根据需要修改的示例函数。 />
Here is the sample function that you need to modify according your requirement.
public static List<periodicalfile> DownloadFTPFiles(string ftpAddress, string UserName, string Password)
{
    FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(ftpAddress);

    try
    {
        reqFTP.UsePassive = true;
        reqFTP.UseBinary = true;
        reqFTP.KeepAlive = false;
        reqFTP.Credentials = new NetworkCredential(UserName, Password);
        reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

        Stream responseStream = response.GetResponseStream();
        List<string> files = new List<string>();
        StreamReader reader = new StreamReader(responseStream);
        while (!reader.EndOfStream)
            files.Add(reader.ReadLine());

        reader.Close();
        responseStream.Dispose();
        List<periodicalfile> lstFiles = new List<periodicalfile>();
        //Loop through the resulting file names.
        string ftpPath = string.Empty;
        foreach (var fileName in files)
        {
            var parentDirectory = "";
            PeriodicalFile lstFile = new PeriodicalFile();
            //If the filename has an extension, then it actually is 
            //a file            
            if (fileName.Contains(".zip"))
            {
                ftpPath = ftpAddress + fileName;
                lstFile.FTPFileName = ftpPath;
                listAllFTPFolder.Add(lstFile);
            }
            else
            {
                //If the filename has no extension, then it is just a folder. 
                //Run this method again as a recursion of the original:
                parentDirectory += fileName + "/";
                try
                {
                    DownloadFTPFiles(ftpAddress + "/" + parentDirectory, UserName, Password);
                }
                catch (Exception ex)
                {
                    ErrorLog.WriteLog("ftpFileProcessing", "DownloadFTPFiles(Else)", ex.Message, ex.StackTrace, "");
                }
            }
        }
    }
    catch (Exception excpt)
    {
        reqFTP.Abort();
        ErrorLog.WriteLog("ftpFileProcessing", "DownloadFTPFiles", excpt.Message, excpt.StackTrace, "");
    }
    return listAllFTPFolder;
}


来自MSDN的示例,希望这有帮助。

Example from MSDN, Hope this helps.
using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main ()
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            Console.WriteLine(reader.ReadToEnd());

            Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
    
            reader.Close();
            response.Close();  
        }
    }
}


这篇关于从FTP服务器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆