1.整理简化了下C#的ftp操作,方便使用

   1.支持创建多级目录

   2.批量删除

   3.整个目录上传

   4.整个目录删除

   5.整个目录下载

2.调用方法展示,

  1. var ftp = new FtpHelper("10.136.12.11", "qdx1213123", "123ddddf");//初始化ftp,创建ftp对象
  2. ftp.DelAll("test");//删除ftptest目录及其目录下的所有文件
  3. ftp.UploadAllFile("F:\\test\\wms.zip");//上传单个文件到指定目录
  4. ftp.UploadAllFile("F:\\test");//将本地test目录的所有文件上传
  5. ftp.DownloadFile("test\\wms.zip", "F:\\test1");//下载单个目录
  6. ftp.DownloadAllFile("test", "F:\\test1");//批量下载整个目录
  7. ftp.MakeDir("aaa\\bbb\\ccc\\ddd");//创建多级目录

3.FtpHelper 代码。

1.异常方法委托,通过Lamda委托统一处理异常,方便改写。加了个委托方便控制异常输出

2.ftp的删除需要递归查找所有目录存入list,然后根据 level倒序排序,从最末级开始遍历删除

3.其他的整个目录操作都是同上

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.IO;
  6.  
  7. namespace MyStuday
  8. {
  9. /// <summary>
  10. /// ftp帮助类
  11. /// </summary>
  12. public class FtpHelper
  13. {
  14. private string ftpHostIP { get; set; }
  15. private string username { get; set; }
  16. private string password { get; set; }
  17. private string ftpURI { get { return $@"ftp://{ftpHostIP}/"; } }
  18.  
  19. /// <summary>
  20. /// 初始化ftp参数
  21. /// </summary>
  22. /// <param name="ftpHostIP">ftp主机IP</param>
  23. /// <param name="username">ftp账户</param>
  24. /// <param name="password">ftp密码</param>
  25. public FtpHelper(string ftpHostIP, string username, string password)
  26. {
  27. this.ftpHostIP = ftpHostIP;
  28. this.username = username;
  29. this.password = password;
  30. }
  31.  
  32. /// <summary>
  33. /// 异常方法委托,通过Lamda委托统一处理异常,方便改写
  34. /// </summary>
  35. /// <param name="method">当前执行的方法</param>
  36. /// <param name="action"></param>
  37. /// <returns></returns>
  38. private bool MethodInvoke(string method, Action action)
  39. {
  40. if (action != null)
  41. {
  42. try
  43. {
  44. action();
  45. //Logger.Write2File($@"FtpHelper.{method}:执行成功");
  46. FluentConsole.Magenta.Line($@"FtpHelper({ftpHostIP},{username},{password}).{method}:执行成功");
  47. return true;
  48. }
  49. catch (Exception ex)
  50. {
  51. FluentConsole.Red.Line($@"FtpHelper({ftpHostIP},{username},{password}).{method}:执行失败:\n {ex}");
  52. // Logger.Write2File(FtpHelper({ftpHostIP},{username},{password}).{method}:执行失败 \r\n{ex}");
  53. return false;
  54. }
  55. }
  56. else
  57. {
  58. return false;
  59. }
  60. }
  61.  
  62. /// <summary>
  63. /// 异常方法委托,通过Lamda委托统一处理异常,方便改写
  64. /// </summary>
  65. /// </summary>
  66. /// <typeparam name="T"></typeparam>
  67. /// <param name="method"></param>
  68. /// <param name="func"></param>
  69. /// <returns></returns>
  70. private T MethodInvoke<T>(string method, Func<T> func)
  71. {
  72. if (func != null)
  73. {
  74. try
  75. {
  76. //FluentConsole.Magenta.Line($@"FtpHelper({ftpHostIP},{username},{password}).{method}:执行成功");
  77. //Logger.Write2File($@"FtpHelper({ftpHostIP},{username},{password}).{method}:执行成功");
  78. return func();
  79. }
  80. catch (Exception ex)
  81. {
  82. //FluentConsole.Red.Line($@"FtpHelper({ftpHostIP},{username},{password}).{method}:执行失败:").Line(ex);
  83. // Logger.Write2File($@"FtpHelper({ftpHostIP},{username},{password}).{method}:执行失败 \r\n{ex}");
  84. return default(T);
  85. }
  86. }
  87. else
  88. {
  89. return default(T);
  90. }
  91. }
  92. private FtpWebRequest GetRequest(string URI)
  93. {
  94. //根据服务器信息FtpWebRequest创建类的对象
  95. FtpWebRequest result = (FtpWebRequest)WebRequest.Create(URI);
  96. result.Credentials = new NetworkCredential(username, password);
  97. result.KeepAlive = false;
  98. result.UsePassive = false;
  99. result.UseBinary = true;
  100. return result;
  101. }
  102.  
  103. /// <summary> 上传文件</summary>
  104. /// <param name="filePath">需要上传的文件路径</param>
  105. /// <param name="dirName">目标路径</param>
  106. public bool UploadFile(string filePath, string dirName = "")
  107. {
  108. FileInfo fileInfo = new FileInfo(filePath);
  109. if (dirName != "") MakeDir(dirName);//检查文件目录,不存在就自动创建
  110. string uri = Path.Combine(ftpURI, dirName, fileInfo.Name);
  111. return MethodInvoke($@"uploadFile({filePath},{dirName})", () =>
  112. {
  113. FtpWebRequest ftp = GetRequest(uri);
  114. ftp.Method = WebRequestMethods.Ftp.UploadFile;
  115. ftp.ContentLength = fileInfo.Length;
  116. int buffLength = ;
  117. byte[] buff = new byte[buffLength];
  118. int contentLen;
  119. using (FileStream fs = fileInfo.OpenRead())
  120. {
  121. using (Stream strm = ftp.GetRequestStream())
  122. {
  123. contentLen = fs.Read(buff, , buffLength);
  124. while (contentLen != )
  125. {
  126. strm.Write(buff, , contentLen);
  127. contentLen = fs.Read(buff, , buffLength);
  128. }
  129. strm.Close();
  130. }
  131. fs.Close();
  132. }
  133. });
  134. }
  135.  
  136. /// <summary>
  137. /// 从一个目录将其内容复制到另一目录
  138. /// </summary>
  139. /// <param name="localDir">源目录</param>
  140. /// <param name="DirName">目标目录</param>
  141. public void UploadAllFile(string localDir, string DirName = "")
  142. {
  143. string localDirName = string.Empty;
  144. int targIndex = localDir.LastIndexOf("\\");
  145. if (targIndex > - && targIndex != (localDir.IndexOf(":\\") + ))
  146. localDirName = localDir.Substring(, targIndex);
  147. localDirName = localDir.Substring(targIndex + );
  148. string newDir = Path.Combine(DirName, localDirName);
  149. MethodInvoke($@"UploadAllFile({localDir},{DirName})", () =>
  150. {
  151. MakeDir(newDir);
  152. DirectoryInfo directoryInfo = new DirectoryInfo(localDir);
  153. FileInfo[] files = directoryInfo.GetFiles();
  154. //复制所有文件
  155. foreach (FileInfo file in files)
  156. {
  157. UploadFile(file.FullName, newDir);
  158. }
  159. //最后复制目录
  160. DirectoryInfo[] directoryInfoArray = directoryInfo.GetDirectories();
  161. foreach (DirectoryInfo dir in directoryInfoArray)
  162. {
  163. UploadAllFile(Path.Combine(localDir, dir.Name), newDir);
  164. }
  165. });
  166. }
  167.  
  168. /// <summary>
  169. /// 删除单个文件
  170. /// </summary>
  171. /// <param name="filePath"></param>
  172. public bool DelFile(string filePath)
  173. {
  174. string uri = Path.Combine(ftpURI, filePath);
  175. return MethodInvoke($@"DelFile({filePath})", () =>
  176. {
  177. FtpWebRequest ftp = GetRequest(uri);
  178. ftp.Method = WebRequestMethods.Ftp.DeleteFile;
  179. FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
  180. response.Close();
  181. });
  182. }
  183.  
  184. /// <summary>
  185. /// 删除最末及空目录
  186. /// </summary>
  187. /// <param name="dirName"></param>
  188. private bool DelDir(string dirName)
  189. {
  190. string uri = Path.Combine(ftpURI, dirName);
  191. return MethodInvoke($@"DelDir({dirName})", () =>
  192. {
  193. FtpWebRequest ftp = GetRequest(uri);
  194. ftp.Method = WebRequestMethods.Ftp.RemoveDirectory;
  195. FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
  196. response.Close();
  197. });
  198. }
  199.  
  200. /// <summary> 删除目录或者其目录下所有的文件 </summary>
  201. /// <param name="dirName">目录名称</param>
  202. /// <param name="ifDelSub">是否删除目录下所有的文件</param>
  203. public bool DelAll(string dirName)
  204. {
  205. var list = GetAllFtpFile(new List<ActFile>(),dirName);
  206. if (list == null) return DelDir(dirName);
  207. if (list.Count==) return DelDir(dirName);//删除当前目录
  208. var newlist = list.OrderByDescending(x => x.level);
  209. foreach (var item in newlist)
  210. {
  211. FluentConsole.Yellow.Line($@"level:{item.level},isDir:{item.isDir},path:{item.path}");
  212. }
  213. string uri = Path.Combine(ftpURI, dirName);
  214. return MethodInvoke($@"DelAll({dirName})", () =>
  215. {
  216. foreach (var item in newlist)
  217. {
  218. if (item.isDir)//判断是目录调用目录的删除方法
  219. DelDir(item.path);
  220. else
  221. DelFile(item.path);
  222. }
  223. DelDir(dirName);//删除当前目录
  224. return true;
  225. });
  226. }
  227.  
  228. /// <summary>
  229. /// 下载单个文件
  230. /// </summary>
  231. /// <param name="ftpFilePath">从ftp要下载的文件路径</param>
  232. /// <param name="localDir">下载至本地路径</param>
  233. /// <param name="filename">文件名</param>
  234. public bool DownloadFile(string ftpFilePath, string saveDir)
  235. {
  236. string filename = ftpFilePath.Substring(ftpFilePath.LastIndexOf("\\") + );
  237. string tmpname = Guid.NewGuid().ToString();
  238. string uri = Path.Combine(ftpURI, ftpFilePath);
  239. return MethodInvoke($@"DownloadFile({ftpFilePath},{saveDir},{filename})", () =>
  240. {
  241. if (!Directory.Exists(saveDir)) Directory.CreateDirectory(saveDir);
  242. FtpWebRequest ftp = GetRequest(uri);
  243. ftp.Method = WebRequestMethods.Ftp.DownloadFile;
  244. using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
  245. {
  246. using (Stream responseStream = response.GetResponseStream())
  247. {
  248. using (FileStream fs = new FileStream(Path.Combine(saveDir, filename), FileMode.CreateNew))
  249. {
  250. byte[] buffer = new byte[];
  251. int read = ;
  252. do
  253. {
  254. read = responseStream.Read(buffer, , buffer.Length);
  255. fs.Write(buffer, , read);
  256. } while (!(read == ));
  257. responseStream.Close();
  258. fs.Flush();
  259. fs.Close();
  260. }
  261. responseStream.Close();
  262. }
  263. response.Close();
  264. }
  265. });
  266. }
  267.  
  268. /// <summary>
  269. /// 从FTP下载整个文件夹
  270. /// </summary>
  271. /// <param name="dirName">FTP文件夹路径</param>
  272. /// <param name="saveDir">保存的本地文件夹路径</param>
  273. public void DownloadAllFile(string dirName, string saveDir)
  274. {
  275. MethodInvoke($@"DownloadAllFile({dirName},{saveDir})", () =>
  276. {
  277. List<ActFile> files = GetFtpFile(dirName);
  278. if (!Directory.Exists(saveDir))
  279. {
  280. Directory.CreateDirectory(saveDir);
  281. }
  282. foreach (var f in files)
  283. {
  284. if (f.isDir) //文件夹,递归查询
  285. {
  286. DownloadAllFile(Path.Combine(dirName,f.name), Path.Combine(saveDir ,f.name));
  287. }
  288. else //文件,直接下载
  289. {
  290. DownloadFile(Path.Combine(dirName,f.name), saveDir);
  291. }
  292. }
  293. });
  294. }
  295.  
  296. /// <summary>
  297. /// 获取当前目录下的目录及文件
  298. /// </summary>
  299. /// param name="ftpfileList"></param>
  300. /// <param name="dirName"></param>
  301. /// <returns></returns>
  302. public List<ActFile> GetFtpFile(string dirName,int ilevel = )
  303. {
  304. var ftpfileList = new List<ActFile>();
  305. string uri = Path.Combine(ftpURI, dirName);
  306. return MethodInvoke($@"GetFtpFile({dirName})", () =>
  307. {
  308. var a = new List<List<string>>();
  309. FtpWebRequest ftp = GetRequest(uri);
  310. ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  311. Stream stream = ftp.GetResponse().GetResponseStream();
  312. using (StreamReader sr = new StreamReader(stream))
  313. {
  314. string line = sr.ReadLine();
  315. while (!string.IsNullOrEmpty(line))
  316. {
  317. ftpfileList.Add(new ActFile { isDir = line.IndexOf("<DIR>") > -, name = line.Substring().Trim(), path = Path.Combine(dirName, line.Substring().Trim()), level= ilevel });
  318. line = sr.ReadLine();
  319. }
  320. sr.Close();
  321. }
  322. return ftpfileList;
  323. });
  324.  
  325. }
  326.  
  327. /// <summary>
  328. /// 获取FTP目录下的所有目录及文件包括其子目录和子文件
  329. /// </summary>
  330. /// param name="result"></param>
  331. /// <param name="dirName"></param>
  332. /// <returns></returns>
  333. public List<ActFile> GetAllFtpFile(List<ActFile> result,string dirName, int level = )
  334. {
  335. var ftpfileList = new List<ActFile>();
  336. string uri = Path.Combine(ftpURI, dirName);
  337. return MethodInvoke($@"GetAllFtpFile({dirName})", () =>
  338. {
  339. ftpfileList = GetFtpFile(dirName, level);
  340. result.AddRange(ftpfileList);
  341. var newlist = ftpfileList.Where(x => x.isDir).ToList();
  342. foreach (var item in newlist)
  343. {
  344. GetAllFtpFile(result,item.path, level+);
  345. }
  346. return result;
  347. });
  348.  
  349. }
  350.  
  351. /// <summary>
  352. /// 检查目录是否存在
  353. /// </summary>
  354. /// <param name="dirName"></param>
  355. /// <param name="currentDir"></param>
  356. /// <returns></returns>
  357. public bool CheckDir(string dirName, string currentDir = "")
  358. {
  359. string uri = Path.Combine(ftpURI, currentDir);
  360. return MethodInvoke($@"CheckDir({dirName}{currentDir})", () =>
  361. {
  362. FtpWebRequest ftp = GetRequest(uri);
  363. ftp.UseBinary = true;
  364. ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  365. Stream stream = ftp.GetResponse().GetResponseStream();
  366. using (StreamReader sr = new StreamReader(stream))
  367. {
  368. string line = sr.ReadLine();
  369. while (!string.IsNullOrEmpty(line))
  370. {
  371. if (line.IndexOf("<DIR>") > -)
  372. {
  373. if (line.Substring().Trim() == dirName)
  374. return true;
  375. }
  376. line = sr.ReadLine();
  377. }
  378. sr.Close();
  379. }
  380. stream.Close();
  381. return false;
  382. });
  383.  
  384. }
  385.  
  386. /// </summary>
  387. /// 在ftp服务器上创建指定目录,父目录不存在则创建
  388. /// </summary>
  389. /// <param name="dirName">创建的目录名称</param>
  390. public bool MakeDir(string dirName)
  391. {
  392. var dirs = dirName.Split('\\').ToList();//针对多级目录分割
  393. string currentDir = string.Empty;
  394. return MethodInvoke($@"MakeDir({dirName})", () =>
  395. {
  396. foreach (var dir in dirs)
  397. {
  398. if (!CheckDir(dir, currentDir))//检查目录不存在则创建
  399. {
  400. currentDir = Path.Combine(currentDir, dir);
  401. string uri = Path.Combine(ftpURI, currentDir);
  402. FtpWebRequest ftp = GetRequest(uri);
  403. ftp.Method = WebRequestMethods.Ftp.MakeDirectory;
  404. FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
  405. response.Close();
  406. }
  407. else
  408. {
  409. currentDir = Path.Combine(currentDir, dir);
  410. }
  411. }
  412.  
  413. });
  414.  
  415. }
  416.  
  417. /// <summary>文件重命名 </summary>
  418. /// <param name="currentFilename">当前名称</param>
  419. /// <param name="newFilename">重命名名称</param>
  420. /// <param name="currentFilename">所在的目录</param>
  421. public bool Rename(string currentFilename, string newFilename, string dirName = "")
  422. {
  423. string uri = Path.Combine(ftpURI, dirName, currentFilename);
  424. return MethodInvoke($@"Rename({currentFilename},{newFilename},{dirName})", () =>
  425. {
  426. FtpWebRequest ftp = GetRequest(uri);
  427. ftp.Method = WebRequestMethods.Ftp.Rename;
  428. ftp.RenameTo = newFilename;
  429. FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
  430. response.Close();
  431. });
  432. }
  433. }
  434.  
  435. public class ActFile
  436. {
  437. public int level { get; set; } = ;
  438. public bool isDir { get; set; }
  439. public string name { get; set; }
  440. public string path { get; set; } = "";
  441. }
  442. }

C#开发-ftp操作方法整理的更多相关文章

  1. ftp操作方法整理

    1.整理简化了下C#的ftp操作,方便使用    1.支持创建多级目录    2.批量删除    3.整个目录上传    4.整个目录删除    5.整个目录下载 2.调用方法展示, var ftp ...

  2. C#开发--FTP操作方法管理

    1.整理简化了下C#的ftp操作,方便使用    1.支持创建多级目录    2.批量删除    3.整个目录上传    4.整个目录删除    5.整个目录下载 2.调用方法展示, var ftp ...

  3. .NET Web开发技术简单整理 转

    .NET Web开发技术简单整理 原文:http://www.cnblogs.com/SanMaoSpace/p/3157293.html 在最初学习一些编程语言.一些编程技术的时候,做的更多的是如何 ...

  4. 我自己总结的C#开发命名规范整理了一份

    我自己总结的C#开发命名规范整理了一份 标签: 开发规范文档标准语言 2014-06-27 22:58 3165人阅读 评论(1) 收藏 举报  分类: C#(39)  版权声明:本文为博主原创文章, ...

  5. Android开发——Fragment知识整理(二)

    0.  前言 Android开发中的Fragment的应用非常广泛,在Android开发--Fragment知识整理(一)中简单介绍了关于Fragment的生命周期,常用API,回退栈的应用等知识.这 ...

  6. Android开发——Fragment知识整理(一)

    0.  前言 Fragment,顾名思义是片段的意思,可以把Fragment当成Activity的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成.Fragment需要被嵌 ...

  7. .NET Web开发技术简单整理

    在最初学习一些编程语言.一些编程技术的时候,做的更多的是如何使用该技术,如何更好的使用该技术解决问题,而没有去关注它的相关性.关注它的理论支持,这种学习技术的方式是短平快.其实工作中有时候也是这样,公 ...

  8. 转载:.NET Web开发技术简单整理

    在最初学习一些编程语言.一些编程技术的时候,做的更多的是如何使用该技术,如何更好的使用该技术解决问题,而没有去关注它的相关性.关注它的理论支持,这种学习技术的方式是短平快.其实工作中有时候也是这样,公 ...

  9. 移动端 Web 开发前端知识整理

    文章来源: http://www.restran.net/2015/05/14/mobile-web-front-end-collections/ 最近整理的移动端 Web 开发前端知识,不定期更新. ...

随机推荐

  1. (八)学习MVC之三级联动

    1.新建项目,MVC选择基本模板 2.新建类:Model/Student.cs,数据库信息有三个实体:分别是年级.班级和学生. using System; using System.Collectio ...

  2. javascript六难题

    1.下面代码的运行效果是什么?为什么? <html> <head> <meta charset="utf-8"> <title>DO ...

  3. A Fast Priority Queue Implementation of the Dijkstra Shortest Path Algorithm

    http://www.codeproject.com/Articles/24816/A-Fast-Priority-Queue-Implementation-of-the-Dijkst http:// ...

  4. C# 创建WebServices及调用方法

    发布WebServices 1.创建  ASP.NET Web 服务应用程序 SayHelloWebServices 这里需要说明一下,由于.NET Framework4.0取消了WebService ...

  5. as3 工具类分享 CookieMgr

    今天分享一个工具类 CookieMgr,功能就是读取和写入 SharedObject 对象.很简单,都是静态方法,就不多说了 package org.polarbear.core { import f ...

  6. Jquery UI的datepicker插件使用方法

    原文链接;http://www.ido321.com/375.html Jquery UI是一个非常丰富的Jquery插件,并且UI的各部分插件可以独自分离出来使用,这是其他很多Jquery插件没有的 ...

  7. python 中 struct 用法

    下面就介绍这个模块中的几个方法. struct.pack():我的理解是,python利用 struct模块将字符(比如说 int,long ,unsized int 等)拆成 字节流(用十六进制表示 ...

  8. 【OpenGL】入门

    根据OpenGL蓝宝书(OpenGL超级宝典)来入门,写的比较细,易懂,这里给我贴代码和记录零碎的事儿用 第一个代码 #include <gl/glut.h> void RenderSce ...

  9. algorithm@ O(3/2 n) time to findmaximum and minimum in a array

    public static int[] max_min(int[] a){ //res[0] records the minimum value while res[1] records the ma ...

  10. 【noip2011】观光公交

    题解: 做这题的时候为了敢速度- - 直接orz了神小黑的题解 其实我还是有想一个拙计的方法的- - dp:f[i][j] 表示到i点使用j个加速器 在i前上车的人的时间和 轻松愉悦转移之 - - 但 ...