如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用。

那就是System.Net.FtpClient,链接地址:https://netftp.codeplex.com

然后下载该资源,我们就可以使用它的函数了。这里介绍一下如何使用System.Net.FtpClient链接ftp服务器并下载服务器中的文件。

千万别忘了添加引用——导入System.Net.FtpClient.dll.

还有就是 using System.Net.FtpClient;

using System.Net;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.FtpClient;
using System.Text;
using System.Threading.Tasks;
using System.IO; namespace FTP_Client
{
public class FTPConnection
{ public FTPConnection() { } /// <summary>
/// 连接FTP服务器函数
/// </summary>
/// <param name="strServer">服务器IP</param>
/// <param name="strUser">用户名</param>
/// <param name="strPassword">密码</param>
public bool FTPIsConnected(string strServer, string strUser, string strPassword)
{
using (FtpClient ftp = new FtpClient())
{
ftp.Host = strServer;
ftp.Credentials = new NetworkCredential(strUser, strPassword);
ftp.Connect();
return ftp.IsConnected;
}
} /// <summary>
/// FTP下载文件
/// </summary>
/// <param name="strServer">服务器IP</param>
/// <param name="strUser">用户名</param>
/// <param name="strPassword">密码</param>
/// <param name="Serverpath">服务器路径,例子:"/Serverpath/"</param>
/// <param name="localpath">本地保存路径</param>
/// <param name="filetype">所下载的文件类型,例子:".rte"</param>
public bool FTPIsdownload(string strServer, string strUser, string strPassword,string Serverpath, string localpath, string filetype)
{ FtpClient ftp = new FtpClient();
ftp.Host = strServer;
ftp.Credentials = new NetworkCredential(strUser, strPassword);
ftp.Connect(); string path = Serverpath;
string destinationDirectory = localpath;
List<string> documentname = new List<string>();
bool DownloadStatus = false; if (Directory.Exists(destinationDirectory))
{
#region 从FTP服务器下载文件
foreach (var ftpListItem in ftp.GetListing(path, FtpListOption.Modify | FtpListOption.Size)
.Where(ftpListItem => string.Equals(Path.GetExtension(ftpListItem.Name), filetype)))
{
string destinationPath = string.Format(@"{0}\{1}", destinationDirectory, ftpListItem.Name);
using (Stream ftpStream = ftp.OpenRead(ftpListItem.FullName))
using (FileStream fileStream = File.Create(destinationPath, (int)ftpStream.Length))
{
var buffer = new byte[ * ];
int count;
while ((count = ftpStream.Read(buffer, , buffer.Length)) > )
{
fileStream.Write(buffer, , count);
}
}
documentname.Add(ftpListItem.Name);
}
#endregion #region 验证本地是否有该文件
string[] files = Directory.GetFiles(localpath, "*"+filetype);
int filenumber = ;
foreach(string strfilename in files)
{
foreach(string strrecievefile in documentname)
{
if (strrecievefile == Path.GetFileName(strfilename))
{
filenumber++;
break;
}
}
}
if(filenumber==documentname.Count)
{
DownloadStatus = true;
}
#endregion
}
return DownloadStatus;
} }
}

注意:如果存放在服务器的文件是放在根目录下,那么服务器路径只需填写“/”,即可。

【FTP】C# System.Net.FtpClient库连接ftp服务器(下载文件)的更多相关文章

  1. 【FTP】C# System.Net.FtpClient库连接ftp服务器(上传文件)

    如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...

  2. Python--paramiko库:连接远程服务器操作文件

    import paramikofrom loggingutils.mylogger import logger as log class SSHConnection(object): def __in ...

  3. (4)FTP服务器下载文件

    上一篇中,我们提到了怎么从FTP服务器下载文件.现在来具体讲述一下. 首先是路径配置.. 所以此处我们需要一个app.config来设置路径. <?xml version="1.0&q ...

  4. 匿名(无账号密码)从ftp服务器下载文件

    public static String downFile(String ip,String ftpFileName,String savePath,String fileName) { FTPCli ...

  5. win端git连接私服仓库+上传本地项目+从服务器下载文件到win

    win端git连接私服仓库: 1.win端 检查c:/Users/用户/.ssh/目录下是否有config文件(!!!没有任何后缀名).如果没有则新建config文件,然后修改添加如下内容: Host ...

  6. SFTP 连接服务器下载文件方法采坑说明

    本篇博客主要记录请求SFTP服务器的一些方法采坑情况. 采坑的方法说明: 1. cd():这个方法用于进入某个目录下. 默认情况,当连接SFTP服务器成功后直接进入用户目录,比如我连接自己本机SFTP ...

  7. Linux连接Windows服务器以及文件传输方法

    Ubantu系统上连接Windows服务器,操作步骤 安装rdesktop sudo apt-get install rdesktop 连接命令 rdesktop -f IP -r disk:mydi ...

  8. linux上创建ftp服务器下载文件///使用AWS服务器作为代理,下载sbt相关的包

    最近觉得自己下载有些jar的速度太慢了,就在aws上下好了,然后转到我电脑上来,在aws上开了ftp服务器.结果就倒腾了一上午,作个记录,以便后面查看. 1.安装vsftpd yum -y insta ...

  9. 如何登陆FTP服务器下载文件

    原文:https://jingyan.baidu.com/article/f25ef254134bef482c1b82c2.html 方法/步骤1   1 第一种介绍的方法是从计算机(我的电脑)上登陆 ...

随机推荐

  1. Unity3D热更新全书-重头再来

    之前写了Unity3D热更新全书系列Blog 提出了下载.加载.脚本三个方面的开源类库 下载方面有EasyDown加载方面有GameObjParser脚本方面有C#Light另外有一个没有独立成库,但 ...

  2. [Beautifulzzzz的博客目录] 快速索引点这儿O(∩_∩)O~~,红色标记的是不错的(⊙o⊙)哦~

    3D相关开发 [direct-X] 1.direct-X最小框架 [OpenGL] 1.环境搭建及最小系统 [OpenGL] 2.企业版VC6.0自带的Win32-OpenGL工程浅析 51单片机 [ ...

  3. Unity3D使用经验总结 优点篇

    09年还在和其它小伙伴开发引擎的时候,Unity3D就初露头角. 当时就对这种基于组件式的设计结构很不理解. 觉得拆分过于细致,同时影响效率. 而时至今日,UNITY3D已经成为了众多团队的首选3D引 ...

  4. AT&T Assembly on Linux

    je if equal then jmp jg if the second gt the first, then jmp jge if the second ge the first, then jm ...

  5. 我的ef连接mysql之旅

      摘要: install-package ef6,mysql.data:增加provider invariantName="MySql.Data.MySqlClient" typ ...

  6. 邮件开发——base64账号密码转换

    package com.hq.base64; import java.io.BufferedReader; import java.io.FileInputStream; import java.io ...

  7. react3 组件

    <body><!-- React 真实 DOM 将会插入到这里 --><div id="example"></div> <!- ...

  8. android 权限大全

    教程 博客 淘帖     论坛›eoe·Android开发资源区›Android开发实例教程 191507 12 / 2 页下一页 android 权限大全 『癲瘋霸気』 于 2013-4-3 10: ...

  9. php手册总结(一)

    一:自动加载 __autoload(): 注意: 1:spl_autoload_register() 提供了一种更加灵活的方式来实现类的自动加载.因此,不再建议使用 __autoload() 函数,在 ...

  10. 一键配置本地yum源

    在使用RedHat系统过程中,经常会安装各种包,而包的安装又存在着依赖性的问题,即一个包的安装通常依赖其它很多包的安装.这样,就会相当不便.所幸,RedHat提供了Yum.Yum(全称为 Yellow ...