C# Ftp Client 上传、下载与删除

简单介绍一下Ftp Client 上传、下载与删除,这是目前比较常用的命令,各个方法其实都差不多,重点是了解Ftp命令协议。

1.建立连接

        public static string Connect(string path, string Login, string Password)
{
try
{
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(path)); //指定命令
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; // 指定数据传输类型
reqFTP.UseBinary = true; // ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(Login, Password); //
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); return "FTP连接成功";
}
catch(Exception ex)
{
return "FTP连接失败," + ex.Message;
} }

2.上传文件

        public static string UploadFile(string filename, string FtpPath, string Login, string Password)
{
try {
FileInfo fileInf = new FileInfo(filename); //判断是否有上级目录 string uri = "ftp://" + Path.Combine(FtpPath, fileInf.Name); // 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(uri)); // 指定数据传输类型
reqFTP.UseBinary = true; // ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(Login, Password); // 默认为true,连接不会被关闭 // 在一个命令之后被执行 reqFTP.KeepAlive = false; // 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // 上传文件时通知服务器文件的大小 reqFTP.ContentLength = fileInf.Length; // 缓冲大小设置为kb int buffLength = ; byte[] buff = new byte[buffLength]; int contentLen; // 打开一个文件流(System.IO.FileStream) 去读上传的文件 FileStream fs = fileInf.OpenRead(); // 把上传的文件写入流 Stream strm = reqFTP.GetRequestStream(); // 每次读文件流的kb contentLen = fs.Read(buff, , buffLength); // 流内容没有结束 while (contentLen != ) { // 把内容从file stream 写入upload stream strm.Write(buff, , contentLen); contentLen = fs.Read(buff, , buffLength); } // 关闭两个流 strm.Close(); fs.Close();
//Successinfo
return string.Format("本地文件{0}已成功上传", fileInf.Name);
} catch (Exception ex) {
//ErrorInfo
return "上传失败" + ex.Message;
} }

3.下载文件

        public static string DownloadFile(string fileDownPath, string fileName, string FtpPath, string Login, string Password)
{
try
{
string onlyFileName = Path.GetFileName(fileName); string newFileName = fileDownPath + onlyFileName; if (File.Exists(newFileName)) { string errorinfo = string.Format("文件{0}在该目录下已存在,无法下载", fileName); return errorinfo;
} string uri = "ftp://" + Path.Combine(FtpPath, fileName); // 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(uri)); // 指定数据传输类型
reqFTP.UseBinary = true; // ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(Login, Password); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int bufferSize = ; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, , bufferSize); FileStream outputStream = new FileStream(newFileName, FileMode.Create); while (readCount > )
{ outputStream.Write(buffer, , readCount); readCount = ftpStream.Read(buffer, , bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); //Successinfo return string.Format("服务器文件{0}已成功下载", fileName); } catch (Exception ex) {
//errorinfo
return string.Format("因{0},无法下载", ex.Message); } }

4.删除文件

        public static string DeleteFile(string fileName, string FtpPath, string Login, string Password)
{
try
{
FileInfo fileInf = new FileInfo(fileName); string uri = "ftp://" + Path.Combine(FtpPath, fileInf.Name); // 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(uri)); // 指定数据传输类型
reqFTP.UseBinary = true; // ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(Login, Password); // 默认为true,连接不会被关闭 // 在一个命令之后被执行 reqFTP.KeepAlive = false; // 指定执行什么命令 reqFTP.Method = WebRequestMethods.Ftp.DeleteFile; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); response.Close(); //Successinfo return string.Format("文件{0}已成功删除", fileInf.Name);
} catch (Exception ex)
{
//ErrorInfo
return string.Format("文件因{0},无法删除", ex.Message);
} }

C# Ftp Client 基本操作的更多相关文章

  1. Ubuntu Filezilla FTP Client 安装

    /************************************************************************************* * Ubuntu File ...

  2. 使用 FileZilla FTP Client连接Vsftpd在执行LIST命令后提示连接超时

    使用 FileZilla FTP Client 连接 Vsftpd在执行LIST命令后提示连接超时. vi /etc/vsftpd/vsftpd.conf 添加: #开启被动模式 pasv_enabl ...

  3. csharp: FTP Client Library using System.Net.FtpWebRequest

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  4. FileZilla FTP Client

    FileZilla Client是一个快速.实用.多功能和界面直观的免费的FTP客户端,虽然它是免费软件,可功能却一点也不含糊,比起那些共享软件来有过之而无不及,在新的版本中作者改进了手动下载的界面和 ...

  5. csharp: FTP Client Library using System.Net.FtpClient and FluentFTP,测试中存在的BUG修正

    https://netftp.codeplex.com/ /// <summary> /// Gets a file listing from the server. Each FtpLi ...

  6. csharp:FTP Client Library using FtpWebRequest or Sockets

    https://netftp.codeplex.com/SourceControl/latest http://ftplib.codeplex.com/ https://www.codeproject ...

  7. FTP Client

    1: /// <summary> 2: /// FTP 管理类 3: /// </summary> 4: public class FTPManage 5: { 6: priv ...

  8. 【FileZilla FTP Client】文件与服务器操作客户端

    跨平台的FTP,FTPS和SFTP客户端 可以断点续传进行上传.下载(需要服务器支持). 自定义命令. 可进行站点管理.

  9. Spring Boot Ftp Client 客户端示例支持断点续传

    本章介绍 Spring Boot 整合 Ftpclient 的示例,支持断点续传 本项目源码下载 1 新建 Spring Boot Maven 示例工程项目 注意:是用来 IDEA 开发工具 File ...

随机推荐

  1. selenium+phantomjs爬取bilibili

    selenium+phantomjs爬取bilibili 首先我们要下载phantomjs 你可以到 http://phantomjs.org/download.html 这里去下载 下载完之后解压到 ...

  2. R-data.table

    data.table可以扩展和增强data.frame的功能,在分组操作和组合时访问速度更快. require(data.table) theDT = data.table(A=1:10, B=let ...

  3. German Collegiate Programming Contest 2015

    // Legacy Code #include <iostream> #include <cstdio> #include <cstring> #include & ...

  4. debian使用ibus

    $ sudo apt-get install ibus ibus-pinyin 点击右上角的键盘图标,设置拼音输入法

  5. JAVA、JDK等入门概念,下载安装JAVA并配置环境变量

    一.概念 Java是一种可以撰写跨平台应用程序的面向对象的程序设计语言,具体介绍可查阅百度JAVA百科,这里不再赘述. Java分为三个体系,分别为: Java SE(J2SE,Java2 Platf ...

  6. python基础学习笔记——网络编程(协议篇)

    一 互联网的本质 咱们先不说互联网是如何通信的(发送数据,文件等),先用一个经典的例子,给大家说明什么是互联网通信. 现在追溯到八九十年代,当时电话刚刚兴起,还没有手机的概念,只是有线电话,那么此时你 ...

  7. Markdown,后缀MD

    Markdown 算是一门新兴语言,现在 7-8 岁了吧.它设计的初衷就是让写字的人专注于写字,用纯文本简单的符号标记格式,最后再通过工具转换成鬼畜的 HTML/XHTML.如果你玩过 wikiped ...

  8. iOS关于Xcode上的Other linker flags

    Targets选项下有Other linker flags的设置,用来填写XCode的链接器参数,如:-ObjC -all_load -force_load等.还记得我们在学习C程序的时候,从C代码到 ...

  9. 解决前端工程师与UI设计协同工作的问题

    前端工程师与UI设计协同工作主要环节在于设计图与前端界面是否一致.(还原度) 不得不说,设计图与前端界面实现不一致的问题时有发生.(好吧,我经验有限)所以经常写完的前端页面都需要去修改.(特别是做移动 ...

  10. [错误处理]python大小写敏感,关键字不要写错

    今天调试程序,发现了一个极为隐蔽的bug. True False关键字大小写写错了,然后半天没找出问题所在.