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. 爬虫制作入门学习笔记2:[转]python爬虫实例项目大全

    WechatSogou [1]- 微信公众号爬虫.基于搜狗微信搜索的微信公众号爬虫接口,可以扩展成基于搜狗搜索的爬虫,返回结果是列表,每一项均是公众号具体信息字典. DouBanSpider [2]- ...

  2. ESP8266入门学习笔记1:资料获取

    乐鑫官网:https://www.espressif.com/zh-hans/products/hardware/esp8266ex/overview 乐鑫资料:https://www.espress ...

  3. graph-Dijkstra's shortest-path alogorithm

    直接贴代码吧,简明易懂. 后面自己写了测试,输入数据为: a b c d e 0 1 4 0 2 2 1 2 3 1 3 2 1 4 3 2 1 1 2 3 4 2 4 5 4 3 1 也就是课本上1 ...

  4. IAR单片机启动文件与程序入口

    最近在做TI单片机TM4C123GE6PZ的BootLoader,需要对启动文件做出修改,折腾了半宿,弄得事实而非. IAR默认提供了单片机的启动文件,cstart.s或者其他cstartxxx.s, ...

  5. 水题:HDU1716-排列2

    排列2 Problem Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代 ...

  6. kettle 增量同步

    http://www.cnblogs.com/inuyasha1027/p/Kettle_update_timestamp.html https://ask.hellobi.com/blog/yugu ...

  7. THUSC2019游记

    Day 0 完全没有明明是最后一次机会的紧张感.大概是滚粗的预兆. 住在西郊.房间好小. 和thupc前一样又有一场cometoj,好像又有小裙子了.upd:改成星空棒棒糖! Day 1 早餐有点棒. ...

  8. nuc 第二届山西省大学生程序设计大赛 魔力手环

    problem 很妙啊--发现状态转移矩阵每一行都可以由上一行平移得到,每次只算第一行然后平移,\(O(n^3)\) 就变成了 \(O(n^2)\). #include <iostream> ...

  9. 到底有没有必要兼容IE版本

    我就说两个字:"没有". 理由如下: 1.占资源空间,额外去写css hack去做页面兼容处理.(主要是增加css代码) PS:css hack 不是W3C的规范,css hack ...

  10. C++ 获取网页源码码的操作

    #include <stdio.h>#include <windows.h>#include <wininet.h>#pragma comment(lib,&quo ...