代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
ftp 上传,下载,删除文件,创建文件夹,删除文夹,重
作者:
dezai
/ 发布于
2014/7/2
/
492
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Net; using System.IO; using System.Web.Configuration; namespace Webftp { public sealed class FtpClientService { #region Internal Members private NetworkCredential certificate; #endregion /// <summary> /// 构造函数,提供初始化数据的功能,打开Ftp站点 /// </summary> public FtpClientService() { certificate = new NetworkCredential(WebConfigurationManager.AppSettings["FtpUserName"], WebConfigurationManager.AppSettings["FtpPassword"]); } /// <summary> /// 创建FTP请求 /// </summary> /// <param name="uri">ftp://myserver/upload.txt</param> /// <param name="method">Upload/Download</param> /// <returns></returns> private FtpWebRequest CreateFtpWebRequest(Uri uri, string method) { FtpWebRequest ftpClientRequest = (FtpWebRequest)WebRequest.Create(uri); ftpClientRequest.Proxy = null; ftpClientRequest.Credentials = certificate; ftpClientRequest.KeepAlive = true; ftpClientRequest.UseBinary = true; ftpClientRequest.UsePassive = true; ftpClientRequest.Method = method; ftpClientRequest.UsePassive = true; //ftpClientRequest.Timeout = -1; return ftpClientRequest; } #region 支持断点续传 public bool UploadFile(string sourceFile, Uri destinationPath, int offSet, string ftpMethod) { try { FileInfo file = new FileInfo(sourceFile); Uri uri = new Uri(destinationPath.AbsoluteUri + "/" + file.Name); FtpWebRequest request = CreateFtpWebRequest(uri, ftpMethod); request.ContentOffset = offSet; Stream requestStream = request.GetRequestStream();//需要获取文件的流 FileStream fileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);//创建存储文件的流 int sourceLength = (int)fileStream.Length; offSet = CopyDataToDestination(fileStream, requestStream, offSet); WebResponse response = request.GetResponse(); response.Close(); if (offSet != 0) { UploadFile(sourceFile, destinationPath, offSet, WebRequestMethods.Ftp.AppendFile); } } catch (Exception ex) { return false; } return true; } private int CopyDataToDestination(Stream sourceStream, Stream destinationStream, int offSet) { try { int sourceLength = (int)sourceStream.Length; int length = sourceLength - offSet; byte[] buffer = new byte[length + offSet]; int bytesRead = sourceStream.Read(buffer, offSet, length); while (bytesRead != 0) { destinationStream.Write(buffer, 0, bytesRead); bytesRead = sourceStream.Read(buffer, 0, length); length = length - bytesRead; offSet = (bytesRead == 0) ? 0 : (sourceLength - length);//(length - bytesRead); } } catch (Exception ex) { string error = ex.ToString(); return offSet; } finally { destinationStream.Close(); sourceStream.Close(); } return offSet; } #endregion //删除文件 public bool DeleteFileName(string deletefile, Uri destinationPath) { try { FileInfo file = new FileInfo(deletefile); Uri uri = new Uri(destinationPath.AbsoluteUri + "/" + file.Name); FtpWebRequest reqFTP = CreateFtpWebRequest(uri, WebRequestMethods.Ftp.DeleteFile); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); response.Close(); return true; } catch (Exception ex) { return false; } } //创建目录 public bool MakeDir(string dirName, Uri destinationPath) { FtpWebResponse response = null; try { FileInfo file = new FileInfo(dirName); Uri uri = new Uri(destinationPath.AbsoluteUri + "/" + file.Name); FtpWebRequest reqFTP = CreateFtpWebRequest(uri, WebRequestMethods.Ftp.MakeDirectory); response = (FtpWebResponse)reqFTP.GetResponse(); response.Close(); return true; } catch (Exception ex) { return false; } } //删除目录 public bool DeleteDir(string dirName, Uri destinationPath) { FtpWebResponse response = null; try { FileInfo file = new FileInfo(dirName); Uri uri = new Uri(destinationPath.AbsoluteUri + "/" + file.Name); FtpWebRequest reqFTP = CreateFtpWebRequest(uri, WebRequestMethods.Ftp.RemoveDirectory); response = (FtpWebResponse)reqFTP.GetResponse(); response.Close(); return true; } catch (Exception ex) { return false; } } //文件改名 public bool Rename(string currentFilename, string newFilename, Uri destinationPath) { try { FileInfo fileInf = new FileInfo(currentFilename); Uri uri = new Uri(destinationPath.AbsoluteUri + "/" + fileInf.Name); FtpWebRequest reqFTP = CreateFtpWebRequest(uri, WebRequestMethods.Ftp.Rename); reqFTP.RenameTo = newFilename; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); response.Close(); return true; } catch (Exception ex) { return false; } } //获得文件 public bool Download(string filePath, string fileName, Uri destinationPath)////上面的代码实现了从ftp服务器下载文件的功能 { try { String onlyFileName = Path.GetFileName(fileName); string newFileName = filePath + "\\" + onlyFileName; if (File.Exists(newFileName)) { return false; } Uri uri = new Uri(destinationPath.AbsoluteUri+onlyFileName ); FtpWebRequest reqFTP = CreateFtpWebRequest(uri, WebRequestMethods.Ftp.DownloadFile); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); FileStream outputStream = new FileStream(newFileName, FileMode.Create); while (readCount > 0) { outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); return true; } catch (Exception ex) { return false; } } } } 调用方法 private FtpClientService ftpClient; ftpClient = new FtpClientService(); string soucepath = Server.MapPath("zippath") + "\\99.zip"; //ftpClient.UploadFile(soucepath, new Uri("ftp://ip/a/"), 0, WebRequestMethods.Ftp.UploadFile); string deltefile = "114.zip"; //ftpClient.DeleteFileName(deltefile ,new Uri("ftp://ip/a")); string dirname = "992"; //ftpClient.MakeDir(dirname, new Uri("ftp://ip/a")); //ftpClient.DeleteDir (dirname, new Uri("ftp://ip/a")); // ftpClient.Rename(deltefile, "00.zip", new Uri("ftp://ip/a")); ftpClient.Download("d:", "00.zip", new Uri("ftp://ip/a/"));
试试其它关键字
ftp
上传
下载
删除文件
创建文件夹
删除文夹
命名
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
dezai
贡献的其它代码
(
1065
)
.
双色球
.
列出所有物理网络适配器
.
快乐数的 Python 实现
.
计算当月还剩天数
.
猜属相
.
二十四小时时钟
.
每日一语
.
很酷的日历
.
超长日历表单
.
最简单的时钟
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3