如果自己单枪匹马写一个连接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. 我YY的一个移动应用运营模式

    尽管自己也还是刚刚毕业不久的前端新人,但网上也不乏一些案例告诉我们有志不在年高,很多优秀的同龄人已经有了不错的成就,所以在切页面写onclick之余也在进行一些思考,前端程序员的出路到底在哪里? 一. ...

  2. 为什么我的新项目选择了Quick-cocos2d-x

    混Quick社区快一周了,还是决定分享一下我选择Quick的原因. 一是向大家介绍一下我自己,同时也希望给大家提供一个参考首先,向大家介绍一下我自己姓名,年龄,性别这些都不重要了.我是一名程序员,在游 ...

  3. Identity自增序列/唯一断标识

    ThreadStatic应用(Identity补完) 用于在高并发环境中的自增序列维护和快速创建唯一不重复的短标识,该类是线程安全的 如在ORM组件中,创建唯一的参数名 特点: 高并发环境下的性能保证 ...

  4. linux下进程间通信

    信号 信号是进程间相互传递消息的一种方法,只是用来通知某进程发生了什么事件,并不给进程传递任何数据. #include <sys/types.h> #include <unistd. ...

  5. IOS Block-Block块的使用与理解

    在IOS中,block块是新添加的语法,其他程序语言中也被称为闭包. 程序块的理念是像任何其他C语言类型一样对待特定的代码块.程序块可以分配给一个变量,以参数的形式传递给函数或方法,当然也可以执行(不 ...

  6. nginx1

    简介: Nginx全程是什么? Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. 安装 配 ...

  7. 22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表。然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法showB输出大写的英文字母表。最后编写主类C,在主类的main方法 中测试类A与类B。

    22.编写一个类A,该类创建的对象可以调用方法showA输出小写的英文字母表.然后再编写一个A类的子类B,子类B创建的对象不仅可以调用方法showA输出小写的英文字母表,而且可以调用子类新增的方法sh ...

  8. [javaweb]Java过滤器与包装设计模式的实用案例.

    在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目 ...

  9. HTML基础笔记-01

    ---恢复内容开始--- 学习网站:W3School 1.基础知识 目录: <1.我的文档—> 选择目录名—> 主页—> 样式(点击标题样式,选择你想要的每个标题,重复此步骤) ...

  10. 前端工程师技能之photoshop巧用系列第四篇——图片格式

    × 目录 [1]图片格式 [2]保存设置 前面的话 对于前端来说,图片格式是需要重要掌握的知识.本文是photoshop巧用系列第四篇——图片格式 图片格式 目前在前端的开发中常用的图片格式有jpg. ...