Нет описания

FTPHelper.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. using System.Web;
  8. /// <summary>
  9. /// FTPHelper 的摘要说明
  10. /// </summary>
  11. public class FTPHelper
  12. {
  13. //基本设置
  14. static private string path = @"ftp://" + Configs.GetValue("ftp") + "/"; //目标路径
  15. static private string ftpip = Configs.GetValue("ftp"); //ftp IP地址
  16. static private string username = Configs.GetValue("account"); //ftp用户名
  17. static private string password = Configs.GetValue("password"); //ftp密码
  18. //获取ftp上面的文件和文件夹
  19. public static string[] GetFileList(string dir)
  20. {
  21. string[] downloadFiles;
  22. StringBuilder result = new StringBuilder();
  23. FtpWebRequest request;
  24. try
  25. {
  26. path = @"ftp://" + Configs.GetValue("ftp") + "/";
  27. if (!string.IsNullOrEmpty(dir))
  28. path = path + dir + "/";
  29. request = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
  30. request.UseBinary = true;
  31. request.Credentials = new NetworkCredential(username, password);//设置用户名和密码
  32. request.Method = WebRequestMethods.Ftp.ListDirectory;
  33. request.UseBinary = true;
  34. WebResponse response = request.GetResponse();
  35. StreamReader reader = new StreamReader(response.GetResponseStream());
  36. string line = reader.ReadLine();
  37. while (line != null)
  38. {
  39. result.Append(line);
  40. result.Append("\n");
  41. Console.WriteLine(line);
  42. line = reader.ReadLine();
  43. }
  44. // to remove the trailing '\n'
  45. result.Remove(result.ToString().LastIndexOf('\n'), 1);
  46. reader.Close();
  47. response.Close();
  48. return result.ToString().Split('\n');
  49. }
  50. catch (Exception ex)
  51. {
  52. Console.WriteLine("获取ftp上面的文件和文件夹:" + ex.Message);
  53. downloadFiles = null;
  54. return downloadFiles;
  55. }
  56. }
  57. /// <summary>
  58. /// 获取文件大小
  59. /// </summary>
  60. /// <param name="file">ip服务器下的相对路径</param>
  61. /// <returns>文件大小</returns>
  62. public static int GetFileSize(string file)
  63. {
  64. StringBuilder result = new StringBuilder();
  65. FtpWebRequest request;
  66. try
  67. {
  68. request = (FtpWebRequest)FtpWebRequest.Create(new Uri(path + file));
  69. request.UseBinary = true;
  70. request.Credentials = new NetworkCredential(username, password);//设置用户名和密码
  71. request.Method = WebRequestMethods.Ftp.GetFileSize;
  72. int dataLength = (int)request.GetResponse().ContentLength;
  73. return dataLength;
  74. }
  75. catch (Exception ex)
  76. {
  77. Console.WriteLine("获取文件大小出错:" + ex.Message);
  78. return -1;
  79. }
  80. }
  81. /// <summary>
  82. /// 文件上传
  83. /// </summary>
  84. /// <param name="filePath">原路径(绝对路径)包括文件名</param>
  85. /// <param name="objPath">目标文件夹:服务器下的相对路径 不填为根目录</param>
  86. public static void FileUpLoad(string filePath, string objPath)
  87. {
  88. try
  89. {
  90. string url = path;
  91. if (objPath != "")
  92. url += objPath + "/";
  93. try
  94. {
  95. FtpWebRequest reqFTP = null;
  96. //待上传的文件 (全路径)
  97. try
  98. {
  99. FileInfo fileInfo = new FileInfo(filePath);
  100. using (FileStream fs = fileInfo.OpenRead())
  101. {
  102. long length = fs.Length;
  103. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url + fileInfo.Name));
  104. //设置连接到FTP的帐号密码
  105. reqFTP.Credentials = new NetworkCredential(username, password);
  106. //设置请求完成后是否保持连接
  107. reqFTP.KeepAlive = false;
  108. //指定执行命令
  109. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  110. //指定数据传输类型
  111. reqFTP.UseBinary = true;
  112. using (Stream stream = reqFTP.GetRequestStream())
  113. {
  114. //设置缓冲大小
  115. int BufferLength = 5120;
  116. byte[] b = new byte[BufferLength];
  117. int i;
  118. while ((i = fs.Read(b, 0, BufferLength)) > 0)
  119. {
  120. stream.Write(b, 0, i);
  121. }
  122. Console.WriteLine("上传文件成功");
  123. }
  124. }
  125. }
  126. catch (Exception ex)
  127. {
  128. Console.WriteLine("上传文件失败错误为" + ex.Message);
  129. }
  130. finally
  131. {
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. Console.WriteLine("上传文件失败错误为" + ex.Message);
  137. }
  138. finally
  139. {
  140. }
  141. }
  142. catch (Exception ex)
  143. {
  144. Console.WriteLine("上传文件失败错误为" + ex.Message);
  145. }
  146. }
  147. /// <summary>
  148. /// 删除文件
  149. /// </summary>
  150. /// <param name="fileName">服务器下的相对路径 包括文件名</param>
  151. public static void DeleteFileName(string fileName)
  152. {
  153. try
  154. {
  155. FileInfo fileInf = new FileInfo(ftpip + "" + fileName);
  156. string uri = path + fileName;
  157. FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  158. // 指定数据传输类型
  159. reqFTP.UseBinary = true;
  160. // ftp用户名和密码
  161. reqFTP.Credentials = new NetworkCredential(username, password);
  162. // 默认为true,连接不会被关闭
  163. // 在一个命令之后被执行
  164. reqFTP.KeepAlive = false;
  165. // 指定执行什么命令
  166. reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
  167. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  168. response.Close();
  169. }
  170. catch (Exception ex)
  171. {
  172. Console.WriteLine("删除文件出错:" + ex.Message);
  173. }
  174. }
  175. /// <summary>
  176. /// 新建目录 上一级必须先存在
  177. /// </summary>
  178. /// <param name="dirName">服务器下的相对路径</param>
  179. public static void MakeDir(string dirName)
  180. {
  181. try
  182. {
  183. string uri = path + dirName;
  184. FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  185. // 指定数据传输类型
  186. reqFTP.UseBinary = true;
  187. // ftp用户名和密码
  188. reqFTP.Credentials = new NetworkCredential(username, password);
  189. reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
  190. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  191. response.Close();
  192. }
  193. catch (Exception ex)
  194. {
  195. Console.WriteLine("创建目录出错:" + ex.Message);
  196. }
  197. }
  198. /// <summary>
  199. /// 删除目录 上一级必须先存在
  200. /// </summary>
  201. /// <param name="dirName">服务器下的相对路径</param>
  202. public static void DelDir(string dirName)
  203. {
  204. try
  205. {
  206. string uri = path + dirName;
  207. FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  208. // ftp用户名和密码
  209. reqFTP.Credentials = new NetworkCredential(username, password);
  210. reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
  211. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  212. response.Close();
  213. }
  214. catch (Exception ex)
  215. {
  216. Console.WriteLine("删除目录出错:" + ex.Message);
  217. }
  218. }
  219. /// <summary>
  220. /// 从ftp服务器上获得文件夹列表
  221. /// </summary>
  222. /// <param name="RequedstPath">服务器下的相对路径</param>
  223. /// <returns></returns>
  224. public static List<string> GetDirctory(string RequedstPath)
  225. {
  226. List<string> strs = new List<string>();
  227. try
  228. {
  229. string uri = path + RequedstPath; //目标路径 path为服务器地址
  230. FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  231. // ftp用户名和密码
  232. reqFTP.Credentials = new NetworkCredential(username, password);
  233. reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  234. WebResponse response = reqFTP.GetResponse();
  235. StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名
  236. string line = reader.ReadLine();
  237. while (line != null)
  238. {
  239. if (line.Contains("<DIR>"))
  240. {
  241. string msg = line.Substring(line.LastIndexOf("<DIR>") + 5).Trim();
  242. strs.Add(msg);
  243. }
  244. line = reader.ReadLine();
  245. }
  246. reader.Close();
  247. response.Close();
  248. return strs;
  249. }
  250. catch (Exception ex)
  251. {
  252. Console.WriteLine("获取目录出错:" + ex.Message);
  253. }
  254. return strs;
  255. }
  256. /// <summary>
  257. /// 从ftp服务器上获得文件列表
  258. /// </summary>
  259. /// <param name="RequedstPath">服务器下的相对路径</param>
  260. /// <returns></returns>
  261. public static List<string> GetFile(string RequedstPath)
  262. {
  263. List<string> strs = new List<string>();
  264. try
  265. {
  266. string uri = path + RequedstPath; //目标路径 path为服务器地址
  267. FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
  268. // ftp用户名和密码
  269. reqFTP.Credentials = new NetworkCredential(username, password);
  270. reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  271. WebResponse response = reqFTP.GetResponse();
  272. StreamReader reader = new StreamReader(response.GetResponseStream());//中文文件名
  273. string line = reader.ReadLine();
  274. while (line != null)
  275. {
  276. if (!line.Contains("<DIR>"))
  277. {
  278. string msg = line.Substring(39).Trim();
  279. strs.Add(msg);
  280. }
  281. line = reader.ReadLine();
  282. }
  283. reader.Close();
  284. response.Close();
  285. return strs;
  286. }
  287. catch (Exception ex)
  288. {
  289. Console.WriteLine("获取文件出错:" + ex.Message);
  290. }
  291. return strs;
  292. }
  293. }