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. 18/07/2017 R matrix

    矩阵:二维数组,元素拥有相同模式(数值型,字符型或者逻辑型) mymatrix <- matrix (vector, nrow_num_of_rows, ncol_num_of_columns, ...

  2. Relu的缺点

    Relu不适合梯度过大的的输入 Relu是我们在训练网络时常用的激活函数之一(对我而言没有之一).然而最近发现Relu太脆弱了,经常由于输入的函数梯度过大导致网络参数更新后,神经元不再有激活功能.特别 ...

  3. OpenCV中的图像形态学转换

    两个基本的形态学操作是腐蚀和膨胀.他们的变化构成了开运算,闭运算,梯度等.下面以这张图为例 1.腐蚀 这个操作会把前景物体的边界腐蚀掉. import cv2 import numpy as np i ...

  4. 用python编写简易登录接口

    需求: 让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后退出程序 可以支持多个用户登录 用户3次认证失败后,退出程序,再次启动程序尝试登陆时,还是锁定状态 下面是我写的代码,如果有BUG或者不 ...

  5. Developing for nRF52810(转载)

    Table of Contents Introduction Hardware emulation of nRF52810 Limitations Software emulation of nRF5 ...

  6. 自动设置IP地址bat脚本

    自动获取IP及DNS: netsh interface ip set address name="本地连接" source=dhcpnetsh interface ip set d ...

  7. Python选修课第二届Turtle绘图大赛~~画猫猫

    (a)20161401167 夏思敏 20161401179 段梦格 (b)代码执行视频链接 点击查看:Python使用turtle库画猫猫 (c)程序源码 import turtle turtle. ...

  8. [转] sublime插件

    Sublime Text 系列 Sublime Text:学习资源篇 Sublime插件:增强篇 Sublime插件:Markdown篇 Sublime插件:C语言篇 Sublime插件:主题篇 Su ...

  9. Xpath - Xpath定位

     selenium 提供的xpath定位方法名为:find_element_by_xpath(xpath表达式) Xpath基本定位语法: / 绝对定位,从根节点选取 // 相对定位,从匹配选择的当前 ...

  10. MySQL 表数据的导入导出

    数据导出 1.  使用 SELECT ...INTO OUTFILE ...命令来导出数据,具体语法如下. mysql> SELECT * FROM tablename INTO OUTFILE ...