【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 第一种介绍的方法是从计算机(我的电脑)上登陆 ...
随机推荐
- Windows上帝模式,上帝应该就是这样使用Windows的
Windows上帝模式(Windows Master Control Panel)由来已久,最早是从Win7优化大湿里看到的一个选项,开启后在桌面生成一个图标,点进去后里面包含了几乎全部Windows ...
- SVN 使用
我是一个前端,svn 的服务器配置也是后端弄好的,到底怎么弄的不清楚. 最开始是想和xcode关联起来,每次提交代码也方便,但是在Xcode里的偏好设置Accounts 模块 添加了SVN 服务端地址 ...
- 关于stm32的正交解码
关于正交解码,我先解释何为正交解码,,,,其实名字挺高大上的,,,,还是先说编码器吧 看一下我用过的一种编码器 编码器的 线 数 ,是说编码器转一圈输出多少个脉冲,,,如果一个编码器是500线,,,说 ...
- maven install 读取jar包时出错;error in opening zip file
错误信息: [INFO] ------------------------------------------------------------------------ [ERROR] Failed ...
- Java override 和 overload 的区别
override 是重写(覆盖)了一个方法,用来实现不同的功能,一般是用于子类继承父类时,重写父类的方法的时候. 重写(覆盖)的规则: 1.重写方法的参数列表必须表示与被重写的方法相同,否则不能称为重 ...
- CSS中模拟父元素选择器
很多情况下,我们需要找到父元素,但可惜的是css中并没有这样的一个选择器. 至于原因可以看张鑫旭的如何在CSS中实现父选择器效果这篇文章. 简单来说这个实现并不是真正的父元素选择器,只是利用其它思路来 ...
- OpenCascade Shape Representation in OpenSceneGraph
OpenCascade Shape Representation in OpenSceneGraph eryar@163.com 摘要Abstract:本文通过程序实例,将OpenCascade中的拓 ...
- IO流-文本IO\读写二进制数据
文本IO 一.简述 OutputStreamWriter类使用选定的编码方式吧Unicode字符流转换为字节流,InputStreamReader类将包含字节的输入流转为可以产生Unicode字符的读 ...
- QQ5.0左侧滑动显示效果
前三篇为大家介绍了如何实现简单的类QQ5.0左侧的侧滑效果,本篇我将带领大家一起探讨一下如何真正实现QQ5.0左侧的侧滑效果,对于本篇的内容与之前的三篇关联性很强,如果前三篇你已经完全掌握,对于这一篇 ...
- 再读GFS论文
http://loopjump.com/gfs_paper_note/ 再读GFS的一些笔记.主要涉及GFS架构.Chunk大小选择的一些折中考量.元数据管理及锁.写数据流程.GFS一致性模型的理解. ...