C#从FTP服务器下载文件到本地

这篇博客介绍了一个使用C#编写的辅助类,该类能够帮助开发者从FTP服务器高效地将文件下载到本地计算机,适用于服务器管理和运维场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

提供一个helper类,从FTP服务器下载文件到本地

public class FTPHelper
    {
   
   
        public static FtpFileInfo[] GetFtpFileInfos(string ftpPath, string userName, string passWord)
        {
   
   
            LinkedList<FtpFileInfo> linkedList = new LinkedList<FtpFileInfo>();
            var reqFtp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));
            reqFtp.UsePassive = false;
            reqFtp.UseBinary = true;
            reqFtp.Credentials = new NetworkCredential(userName, passWord);
            reqFtp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            var response = (FtpWebResponse)reqFtp.GetResponse();
            var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string fileDetail = reader.ReadLine();
            while (fileDetail != null)
            {
   
   
                linkedList.AddLast(new FtpFileInfo(fileDetail));
                fileDetail = reader.ReadLine();
            }
            reader.Close();
            response.Close();
            return linkedList.ToArray();
        }
        /// <summary>
        /// 从ftp下载文件夹到本地
        /// </summary>
        /// <param name="sourceDirectoryPath">源数据文件夹地址(备注:结尾不要“/”)</param>
        /// <param name="targetDirectoryPath">目标数据文件夹地址(备注:结尾不要“/”)</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        public static bool DownLoadDirectory(string sourceDirectoryPath, string targetDirectoryPath, string username, string password)
        {
   
   
            try
            {
   
   
                var ftpFileInfos = GetFtpFileInfos(sourceDirectoryPath, username, password);
                if (ftpFileInfos != null && ftpFileInfos.Count() > 0)
                {
   
   
                    foreach (var file in ftpFileInfos)
                    {
   
   
                        string fullSourcePath = sourceDirectoryPath + "/" + file.FileName;
                        string fullTargetPath = targetDirectoryPath + "/" + file.FileName;
                        if (file.UnixFileType == "d")
                        {
   
   
                            if (!string.IsNullOrEmpty(file.FileName.Replace(".", "")))
                            {
   
   
                                DownLoadDirectory(fullSourcePath, fullTargetPath, username, password);
                            }
                        }
                        else
                        {
   
   
                            DownLoadFile(fullSourcePath, targetDirectoryPath, username, password);
                        }
                    }
                }
                return true;
            }catch
            {
   
   
                return false;
            }
            
        }
        /// <summary>
        /// 从ftp下载文件到本地文件夹
        /// </summary>
        /// <param name="fullSourcePath">ftp文件完整地址</param>
        /// <param name="targetDirectoryPath">目标文件夹路径</param>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        public static void DownLoadFile(string
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值