【FTP】C# System.Net.FtpClient库连接ftp服务器(下载文件)
如果自己单枪匹马写一个连接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服务器(下载文件)的更多相关文章
- 【FTP】C# System.Net.FtpClient库连接ftp服务器(上传文件)
如果自己单枪匹马写一个连接ftp服务器代码那是相当恐怖的(socket通信),有一个评价较高的dll库可以供我们使用. 那就是System.Net.FtpClient,链接地址:https://net ...
- Python--paramiko库:连接远程服务器操作文件
import paramikofrom loggingutils.mylogger import logger as log class SSHConnection(object): def __in ...
- (4)FTP服务器下载文件
上一篇中,我们提到了怎么从FTP服务器下载文件.现在来具体讲述一下. 首先是路径配置.. 所以此处我们需要一个app.config来设置路径. <?xml version="1.0&q ...
- 匿名(无账号密码)从ftp服务器下载文件
public static String downFile(String ip,String ftpFileName,String savePath,String fileName) { FTPCli ...
- win端git连接私服仓库+上传本地项目+从服务器下载文件到win
win端git连接私服仓库: 1.win端 检查c:/Users/用户/.ssh/目录下是否有config文件(!!!没有任何后缀名).如果没有则新建config文件,然后修改添加如下内容: Host ...
- SFTP 连接服务器下载文件方法采坑说明
本篇博客主要记录请求SFTP服务器的一些方法采坑情况. 采坑的方法说明: 1. cd():这个方法用于进入某个目录下. 默认情况,当连接SFTP服务器成功后直接进入用户目录,比如我连接自己本机SFTP ...
- Linux连接Windows服务器以及文件传输方法
Ubantu系统上连接Windows服务器,操作步骤 安装rdesktop sudo apt-get install rdesktop 连接命令 rdesktop -f IP -r disk:mydi ...
- linux上创建ftp服务器下载文件///使用AWS服务器作为代理,下载sbt相关的包
最近觉得自己下载有些jar的速度太慢了,就在aws上下好了,然后转到我电脑上来,在aws上开了ftp服务器.结果就倒腾了一上午,作个记录,以便后面查看. 1.安装vsftpd yum -y insta ...
- 如何登陆FTP服务器下载文件
原文:https://jingyan.baidu.com/article/f25ef254134bef482c1b82c2.html 方法/步骤1 1 第一种介绍的方法是从计算机(我的电脑)上登陆 ...
随机推荐
- Html做三个平台原生APP啦
DeviceOne之前介绍过了,现在来介绍一下DeviceOne快速开发到什么程度 使用js只需要5分钟就可以打出垮Android.ios.windows三大平台的纯原生UI的安装包. 只需要6个小时 ...
- 团队项目——站立会议 DAY11
团队项目--站立会议 DAY11 团队成员介绍(5人):张靖颜.何玥.钟灵毓秀.赵莹.王梓萱 今日(2016/5/20),站立会议已进行了两周时间,将这一周所遇到的问题和心 ...
- 用手机访问本地环境的利器ngrok
我们在调试移动端页面,尤其是调试微信页面的时候,会遇到这样的情况,手机连着公司的wifi,开发环境(台式机)插着公司的网线,我们要用手机来访问开发环境下的页面.这个时候,如果网线和wifi是在同一个网 ...
- Linux多线程系列-2-条件变量的使用(线程安全队列的实现)
多线程情况下,往往需要使用互斥变量来实现线程间的同步,实现资源正确共享. linux下使用如下变量和函数 //条件变量 pthread_cond_t int pthread_cond_init (pt ...
- Vue的一个陷阱
最近做项目,上线前一直有个bug,不知道是什么原因引起的, vm.$set('needVerification', true); $('.verification-button').prop('dis ...
- Java-继承,多态练习0922-03
编写一个Java应用程序,该程序包括3个类:Monkey类.People类和主类 E.要求: (1) Monkey类中有个构造方法:Monkey (String s),并且有个public void ...
- 知方可补不足~利用LogParser将IIS日志插入到数据库
回到目录 LogParser是微软开发的一个日志分析工具,它是命令行格式的,我们通过这个工具,可以对日志文件进行操作,对于一个几百兆的log文件,使用记事本打开是件很残酷的事,所以,很多情况下,我们都 ...
- 基础才是重中之重~ConcurrentDictionary让你的多线程代码更优美
回到目录 ConcurrentDictionary是.net4.0推出的一套线程安全集合里的其中一个,和它一起被发行的还有ConcurrentStack,ConcurrentQueue等类型,它们的单 ...
- 01- Shell脚本学习--入门
简介 Shell是一种脚本语言,那么,就必须有解释器来执行这些脚本. Unix/Linux上常见的Shell脚本解释器有bash.sh.csh.ksh等,习惯上把它们称作一种Shell.我们常说有多少 ...
- Memcache服务器端参数说明
/usr/local/webserver/memcached/bin/memcached -d -m 512 -u root -l 192.168.0.1 -p 11211 -c 512 -P /us ...