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. Java观察者模式(Observer)

    一.定义 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态上发生变化时,会通知所有观察者对象,让他们能够自动更新自己.主要应用在java的AWT事件机制 ...

  2. PHP GD库---之商详合成分享图片

    $item_pic = 'img/item.jpg'; $qcode_pic = 'img/qcode.png'; $user_pic = 'img/user.jpeg'; $item_title = ...

  3. 浅谈CSS中的百分比

    结论: 标准流中的元素,看其属性有没有继承性.对于width和margin-left,它是可以继承的,它会参照父元素或者祖先元素(其实是包含块):对于height,它没有继承性,父元素或者祖先元素会自 ...

  4. tar.xz结尾的文件的解压缩方法

    例如: codeblocks-13.12-1_i386.debian.stable.tar 这个压缩包也是两层压缩,外面是xz压缩方式,里层是tar压缩方式. 解压缩方法: $xz -d ***.ta ...

  5. 利用Python分析羊车门问题

    题目描述:有3扇关闭的门,一扇门后面停着汽车,其余门后是山羊,只有主持人知道每扇门后面是什么.参赛者可以选择一扇门,在开启它之前,主持人会开启另外一扇门,露出门后的山羊,然后允许参赛者更换自己的选择. ...

  6. CM10 WIFI连不上解决方案

    手机是Moto Mileston2 ,好久之前就刷成了CM10, 但是一直没出问题. 最近,发现在某些路由器上连接不上,总是 在验证账户或者获取IP. 解决办法如下: http://moto.zol. ...

  7. [python IO学习篇] 补充中文编码

    http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386820066616a7 ...

  8. 使用sami生成文档

    从composer安装sami $ composer require sami/sami composer自动配置完以后,可以先测试一下是否安装成功.只要不带参数的运行一下sami,就会知道结果. $ ...

  9. 九度oj 题目1084:整数拆分 清华大学2010年机试题目

    题目描述: 一个整数总可以拆分为2的幂的和,例如:7=1+2+4 7=1+2+2+2 7=1+1+1+4 7=1+1+1+2+2 7=1+1+1+1+1+2 7=1+1+1+1+1+1+1总共有六种不 ...

  10. 算法golang篇

    1.slice反转,偏移 func reverse(s []int) { , len(s) - ; i < j; i, j = i+, j- { s[i], s[j] = s[j], s[i] ...