C#一个FTP操作封装类FTPHelper
参考了网上一些代码,作了一些调整优化。
001 |
using System; |
002 |
using System.Collections.Generic; |
003 |
using System.Linq; |
004 |
using System.Text; |
005 |
using System.Net; |
006 |
using System.IO; |
007 |
008 |
public class FTPHelper |
009 |
{ |
010 |
/// <summary> |
011 |
/// FTP请求对象 |
012 |
/// </summary> |
013 |
FtpWebRequest request = null ; |
014 |
/// <summary> |
015 |
/// FTP响应对象 |
016 |
/// </summary> |
017 |
FtpWebResponse response = null ; |
018 |
/// <summary> |
019 |
/// FTP服务器地址 |
020 |
/// </summary> |
021 |
public string ftpURI { get ; private set ; } |
022 |
/// <summary> |
023 |
/// FTP服务器IP |
024 |
/// </summary> |
025 |
public string ftpServerIP { get ; private set ; } |
026 |
/// <summary> |
027 |
/// FTP服务器默认目录 |
028 |
/// </summary> |
029 |
public string ftpRemotePath { get ; private set ; } |
030 |
/// <summary> |
031 |
/// FTP服务器登录用户名 |
032 |
/// </summary> |
033 |
public string ftpUserID { get ; private set ; } |
034 |
/// <summary> |
035 |
/// FTP服务器登录密码 |
036 |
/// </summary> |
037 |
public string ftpPassword { get ; private set ; } |
038 |
039 |
/// <summary> |
040 |
/// 初始化 |
041 |
/// </summary> |
042 |
/// <param name="FtpServerIP">FTP连接地址</param> |
043 |
/// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param> |
044 |
/// <param name="FtpUserID">用户名</param> |
045 |
/// <param name="FtpPassword">密码</param> |
046 |
public FTPHelper( string ftpServerIP, string ftpRemotePath, string ftpUserID, string ftpPassword) |
047 |
{ |
048 |
this .ftpServerIP = ftpServerIP; |
049 |
this .ftpRemotePath = ftpRemotePath; |
050 |
this .ftpUserID = ftpUserID; |
051 |
this .ftpPassword = ftpPassword; |
052 |
this .ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/" ; |
053 |
} |
054 |
~FTPHelper() |
055 |
{ |
056 |
if (response != null ) |
057 |
{ |
058 |
response.Close(); |
059 |
response = null ; |
060 |
} |
061 |
if (request != null ) |
062 |
{ |
063 |
request.Abort(); |
064 |
request = null ; |
065 |
} |
066 |
} |
067 |
/// <summary> |
068 |
/// 建立FTP链接,返回响应对象 |
069 |
/// </summary> |
070 |
/// <param name="uri">FTP地址</param> |
071 |
/// <param name="ftpMethod">操作命令</param> |
072 |
/// <returns></returns> |
073 |
private FtpWebResponse Open(Uri uri, string ftpMethod) |
074 |
{ |
075 |
request = (FtpWebRequest)FtpWebRequest.Create(uri); |
076 |
request.Method = ftpMethod; |
077 |
request.UseBinary = true ; |
078 |
request.KeepAlive = false ; |
079 |
request.Credentials = new NetworkCredential( this .ftpUserID, this .ftpPassword); |
080 |
return (FtpWebResponse)request.GetResponse(); |
081 |
} |
082 |
083 |
/// <summary> |
084 |
/// 建立FTP链接,返回请求对象 |
085 |
/// </summary> |
086 |
/// <param name="uri">FTP地址</param> |
087 |
/// <param name="ftpMethod">操作命令</param> |
088 |
private FtpWebRequest OpenRequest(Uri uri, string ftpMethod) |
089 |
{ |
090 |
request = (FtpWebRequest)WebRequest.Create(uri); |
091 |
request.Method = ftpMethod; |
092 |
request.UseBinary = true ; |
093 |
request.KeepAlive = false ; |
094 |
request.Credentials = new NetworkCredential( this .ftpUserID, this .ftpPassword); |
095 |
return request; |
096 |
} |
097 |
/// <summary> |
098 |
/// 创建目录 |
099 |
/// </summary> |
100 |
/// <param name="remoteDirectoryName">目录名</param> |
101 |
public void CreateDirectory( string remoteDirectoryName) |
102 |
{ |
103 |
response = Open( new Uri(ftpURI + remoteDirectoryName), WebRequestMethods.Ftp.MakeDirectory); |
104 |
} |
105 |
/// <summary> |
106 |
/// 更改目录或文件名 |
107 |
/// </summary> |
108 |
/// <param name="currentName">当前名称</param> |
109 |
/// <param name="newName">修改后新名称</param> |
110 |
public void ReName( string currentName, string newName) |
111 |
{ |
112 |
request = OpenRequest( new Uri(ftpURI + currentName), WebRequestMethods.Ftp.Rename); |
113 |
request.RenameTo = newName; |
114 |
response = (FtpWebResponse)request.GetResponse(); |
115 |
} |
116 |
/// <summary> |
117 |
/// 切换当前目录 |
118 |
/// </summary> |
119 |
/// <param name="IsRoot">true:绝对路径 false:相对路径</param> |
120 |
public void GotoDirectory( string DirectoryName, bool IsRoot) |
121 |
{ |
122 |
if (IsRoot) |
123 |
ftpRemotePath = DirectoryName; |
124 |
else |
125 |
ftpRemotePath += "/" + DirectoryName; |
126 |
127 |
ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/" ; |
128 |
} |
129 |
/// <summary> |
130 |
/// 删除目录(包括下面所有子目录和子文件) |
131 |
/// </summary> |
132 |
/// <param name="remoteDirectoryName">要删除的带路径目录名:如web/test</param> |
133 |
/* |
134 |
* 例:删除test目录 |
135 |
FTPHelper helper = new FTPHelper("x.x.x.x", "web", "user", "password"); |
136 |
helper.RemoveDirectory("web/test"); |
137 |
*/ |
138 |
public void RemoveDirectory( string remoteDirectoryName) |
139 |
{ |
140 |
GotoDirectory(remoteDirectoryName, true ); |
141 |
var listAll = ListFilesAndDirectories(); |
142 |
foreach (var m in listAll) |
143 |
{ |
144 |
if (m.IsDirectory) |
145 |
RemoveDirectory(m.Path); |
146 |
else |
147 |
DeleteFile(m.Name); |
148 |
} |
149 |
GotoDirectory(remoteDirectoryName, true ); |
150 |
response = Open( new Uri(ftpURI), WebRequestMethods.Ftp.RemoveDirectory); |
151 |
} |
152 |
/// <summary> |
153 |
/// 文件上传 |
154 |
/// </summary> |
155 |
/// <param name="localFilePath">本地文件路径</param> |
156 |
public void Upload( string localFilePath) |
157 |
{ |
158 |
FileInfo fileInf = new FileInfo(localFilePath); |
159 |
request = OpenRequest( new Uri(ftpURI + fileInf.Name), WebRequestMethods.Ftp.UploadFile); |
160 |
request.ContentLength = fileInf.Length; |
161 |
int buffLength = 2048; |
162 |
byte [] buff = new byte [buffLength]; |
163 |
int contentLen; |
164 |
using (var fs = fileInf.OpenRead()) |
165 |
{ |
166 |
using (var strm = request.GetRequestStream()) |
167 |
{ |
168 |
contentLen = fs.Read(buff, 0, buffLength); |
169 |
while (contentLen != 0) |
170 |
{ |
171 |
strm.Write(buff, 0, contentLen); |
172 |
contentLen = fs.Read(buff, 0, buffLength); |
173 |
} |
174 |
} |
175 |
} |
176 |
} |
177 |
/// <summary> |
178 |
/// 删除文件 |
179 |
/// </summary> |
180 |
/// <param name="remoteFileName">要删除的文件名</param> |
181 |
public void DeleteFile( string remoteFileName) |
182 |
{ |
183 |
response = Open( new Uri(ftpURI + remoteFileName), WebRequestMethods.Ftp.DeleteFile); |
184 |
} |
185 |
186 |
/// <summary> |
187 |
/// 获取当前目录的文件和一级子目录信息 |
188 |
/// </summary> |
189 |
/// <returns></returns> |
190 |
public List<FileStruct> ListFilesAndDirectories() |
191 |
{ |
192 |
var fileList = new List<FileStruct>(); |
193 |
response = Open( new Uri(ftpURI), WebRequestMethods.Ftp.ListDirectoryDetails); |
194 |
using (var stream = response.GetResponseStream()) |
195 |
{ |
196 |
using (var sr = new StreamReader(stream)) |
197 |
{ |
198 |
string line = null ; |
199 |
while ((line = sr.ReadLine()) != null ) |
200 |
{ |
201 |
//line的格式如下: |
202 |
//08-18-13 11:05PM <DIR> aspnet_client |
203 |
//09-22-13 11:39PM 2946 Default.aspx |
204 |
DateTime dtDate = DateTime.ParseExact(line.Substring(0, 8), "MM-dd-yy" , null ); |
205 |
DateTime dtDateTime = DateTime.Parse(dtDate.ToString( "yyyy-MM-dd" ) + line.Substring(8, 9)); |
206 |
string [] arrs = line.Split( ' ' ); |
207 |
var model = new FileStruct() |
208 |
{ |
209 |
IsDirectory = line.IndexOf( "<DIR>" ) > 0 ? true : false , |
210 |
CreateTime = dtDateTime, |
211 |
Name = arrs[arrs.Length - 1], |
212 |
Path = ftpRemotePath + "/" + arrs[arrs.Length - 1] |
213 |
}; |
214 |
fileList.Add(model); |
215 |
} |
216 |
} |
217 |
} |
218 |
return fileList; |
219 |
} |
220 |
/// <summary> |
221 |
/// 列出当前目录的所有文件 |
222 |
/// </summary> |
223 |
public List<FileStruct> ListFiles() |
224 |
{ |
225 |
var listAll = ListFilesAndDirectories(); |
226 |
var listFile = listAll.Where(m => m.IsDirectory == false ).ToList(); |
227 |
return listFile; |
228 |
} |
229 |
/// <summary> |
230 |
/// 列出当前目录的所有一级子目录 |
231 |
/// </summary> |
232 |
public List<FileStruct> ListDirectories() |
233 |
{ |
234 |
var listAll = ListFilesAndDirectories(); |
235 |
var listFile = listAll.Where(m => m.IsDirectory == true ).ToList(); |
236 |
return listFile; |
237 |
} |
238 |
/// <summary> |
239 |
/// 判断当前目录下指定的子目录或文件是否存在 |
240 |
/// </summary> |
241 |
/// <param name="remoteName">指定的目录或文件名</param> |
242 |
public bool IsExist( string remoteName) |
243 |
{ |
244 |
var list = ListFilesAndDirectories(); |
245 |
if (list.Count(m => m.Name == remoteName) > 0) |
246 |
return true ; |
247 |
return false ; |
248 |
} |
249 |
/// <summary> |
250 |
/// 判断当前目录下指定的一级子目录是否存在 |
251 |
/// </summary> |
252 |
/// <param name="RemoteDirectoryName">指定的目录名</param> |
253 |
public bool IsDirectoryExist( string remoteDirectoryName) |
254 |
{ |
255 |
var listDir = ListDirectories(); |
256 |
if (listDir.Count(m => m.Name == remoteDirectoryName) > 0) |
257 |
return true ; |
258 |
return false ; |
259 |
} |
260 |
/// <summary> |
261 |
/// 判断当前目录下指定的子文件是否存在 |
262 |
/// </summary> |
263 |
/// <param name="RemoteFileName">远程文件名</param> |
264 |
public bool IsFileExist( string remoteFileName) |
265 |
{ |
266 |
var listFile = ListFiles(); |
267 |
if (listFile.Count(m => m.Name == remoteFileName) > 0) |
268 |
return true ; |
269 |
return false ; |
270 |
} |
271 |
272 |
/// <summary> |
273 |
/// 下载 |
274 |
/// </summary> |
275 |
/// <param name="saveFilePath">下载后的保存路径</param> |
276 |
/// <param name="downloadFileName">要下载的文件名</param> |
277 |
public void Download( string saveFilePath, string downloadFileName) |
278 |
{ |
279 |
using (FileStream outputStream = new FileStream(saveFilePath + "\\" + downloadFileName, FileMode.Create)) |
280 |
{ |
281 |
response = Open( new Uri(ftpURI + downloadFileName), WebRequestMethods.Ftp.DownloadFile); |
282 |
using (Stream ftpStream = response.GetResponseStream()) |
283 |
{ |
284 |
long cl = response.ContentLength; |
285 |
int bufferSize = 2048; |
286 |
int readCount; |
287 |
byte [] buffer = new byte [bufferSize]; |
288 |
readCount = ftpStream.Read(buffer, 0, bufferSize); |
289 |
while (readCount > 0) |
290 |
{ |
291 |
outputStream.Write(buffer, 0, readCount); |
292 |
readCount = ftpStream.Read(buffer, 0, bufferSize); |
293 |
} |
294 |
} |
295 |
} |
296 |
} |
297 |
298 |
|
299 |
} |
300 |
301 |
public class FileStruct |
302 |
{ |
303 |
/// <summary> |
304 |
/// 是否为目录 |
305 |
/// </summary> |
306 |
public bool IsDirectory { get ; set ; } |
307 |
/// <summary> |
308 |
/// 创建时间 |
309 |
/// </summary> |
310 |
public DateTime CreateTime { get ; set ; } |
311 |
/// <summary> |
312 |
/// 文件或目录名称 |
313 |
/// </summary> |
314 |
public string Name { get ; set ; } |
315 |
/// <summary> |
316 |
/// 路径 |
317 |
/// </summary> |
318 |
public string Path { get ; set ; } |
319 |
} |
C#一个FTP操作封装类FTPHelper的更多相关文章
- 【C#】工具类-FTP操作封装类FTPHelper
转载:http://blog.csdn.net/gdjlc/article/details/11968477 using System; using System.Collections.Generi ...
- 【转载】C#工具类:FTP操作辅助类FTPHelper
FTP是一个8位的客户端-服务器协议,能操作任何类型的文件而不需要进一步处理,就像MIME或Unicode一样.可以通过C#中的FtpWebRequest类.NetworkCredential类.We ...
- FtpHelper ftp操作类库
FtpHelper ftp操作类库 using System; using System.Collections.Generic; using System.Linq; using System.Te ...
- [转]C# FTP操作类
转自 http://www.cnblogs.com/Liyuting/p/7084718.html using System; using System.Collections.Generic; ...
- [PHP学习教程 - 类库]002.FTP操作(FTP)
引言:FTP是大家上传至站点服务器必须要使用的协议.现在常用的FTP客户端工具也很多,如:8uftp,FlashFXP,....但是使用客户端工具就无法真正与自动化联系起来.所以今天,我们为大家讲一下 ...
- FTP操作类
using System; using System.Collections.Generic; using System.Net; using System.IO; namespace HGFTP { ...
- C# FTP操作
using System; using System.Collections.Generic; using System.Net; using System.IO; namespace FTP操作 { ...
- 基于线程开发一个FTP服务器
一,项目题目:基于线程开发一个FTP服务器 二,项目要求: 基本要求: 1.用户加密认证 2.允许同时多用户登录 3.每个用户有自己的家目录 ,且只能访问自己的家目录 4.对用户进行磁盘配 ...
- Linux中搭建一个ftp服务器详解
来源:Linux社区 作者:luzhi1024 详解Linux中搭建一个ftp服务器. ftp工作是会启动两个通道:控制通道 , 数据通道在ftp协议中,控制连接均是由客户端发起的,而数据连接有两种 ...
随机推荐
- shell 统计行数
语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...
- Spring 定时操作业务需求
1.定时分析 在业务需求中有的需要检测用户的状态,通过对用户状态的检测做出对此状态相应的操作,如果这种检测由运营人工检测,不仅工作量大,而且准确性不高,人工无法很好的完成工作: 问题根源:在检测用户状 ...
- web服务端安全之分布式拒绝服务攻击
一.DDOS攻击的原理分布式拒绝服务,Distributed Denial of Service,利用目标系统网络服务功能缺陷或者直接消耗其系统资源,使得该目标系统无法提供正常的服务.通过大量合法的请 ...
- 1011 World Cup Betting (20)(20 point(s))
problem With the 2010 FIFA World Cup running, football fans the world over were becoming increasingl ...
- [BZOJ3507][CQOI2014]通配符匹配(DP+Hash)
显然f[i][j]表示S匹配到第i个通配符,T匹配到第j个字符,是否可行. 一次一起转移两个通配符之间的所有字符,Hash判断. 稍微有点细节.常数极大卡时过排名倒数,可能是没自然溢出的原因. #in ...
- [BZOJ3595][SCOI2014]方伯伯的OJ(裂点Splay)
用一棵Splay按名次维护每个点,其中一个节点对应初始编号连续的一段区间,这样总节点数是$O(m)$的. 对每个编号记录这个点被Splay的那个节点维护,用std::map存储,只记录被修改的点. 每 ...
- poj 3667 线段树
题意:1 a:询问是不是有连续长度为a的空房间,有的话住进最左边2 a b:将[a,a+b-1]的房间清空思路:记录区间中最长的空房间线段树操作:update:区间替换 query:询问满足条件的最左 ...
- 从数组中查看某值是否存在,Arrays.binarySearch
Arrays.binarySearch为二分法查询,注意:需要排序 使用示例 Arrays.binarySearch(selectedRows, i) >= 0
- IO流-复制多极文件夹(递归实现)
package com.io.test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...
- C++反汇编-继承和多重继承
学无止尽,积土成山,积水成渊-<C++反汇编与逆向分析技术揭秘> 读书笔记 一.单类继承 在父类中声明为私有的成员,子类对象无法直接访问,但是在子类对象的内存结构中,父类私有的成员数据依然 ...