FTPOperater.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. namespace Ant.Service.Utilities
  5. {
  6. public class FTPOperater
  7. {
  8. #region 属性
  9. private FTPClient ftp;
  10. /// <summary>
  11. /// 全局FTP访问变量
  12. /// </summary>
  13. public FTPClient Ftp
  14. {
  15. get { return ftp; }
  16. set { ftp = value; }
  17. }
  18. private string _server;
  19. /// <summary>
  20. /// Ftp服务器
  21. /// </summary>
  22. public string Server
  23. {
  24. get { return _server; }
  25. set { _server = value; }
  26. }
  27. private string _User;
  28. /// <summary>
  29. /// Ftp用户
  30. /// </summary>
  31. public string User
  32. {
  33. get { return _User; }
  34. set { _User = value; }
  35. }
  36. private string _Pass;
  37. /// <summary>
  38. /// Ftp密码
  39. /// </summary>
  40. public string Pass
  41. {
  42. get { return _Pass; }
  43. set { _Pass = value; }
  44. }
  45. private string _FolderZJ;
  46. /// <summary>
  47. /// Ftp密码
  48. /// </summary>
  49. public string FolderZJ
  50. {
  51. get { return _FolderZJ; }
  52. set { _FolderZJ = value; }
  53. }
  54. private string _FolderWX;
  55. /// <summary>
  56. /// Ftp密码
  57. /// </summary>
  58. public string FolderWX
  59. {
  60. get { return _FolderWX; }
  61. set { _FolderWX = value; }
  62. }
  63. #endregion
  64. /// <summary>
  65. /// 得到文件列表
  66. /// </summary>
  67. /// <returns></returns>
  68. public string[] GetList(string strPath)
  69. {
  70. if (ftp == null) ftp = this.getFtpClient();
  71. ftp.Connect();
  72. ftp.ChDir(strPath);
  73. return ftp.Dir("*");
  74. }
  75. /// <summary>
  76. /// 下载文件
  77. /// </summary>
  78. /// <param name="ftpFolder">ftp目录</param>
  79. /// <param name="ftpFileName">ftp文件名</param>
  80. /// <param name="localFolder">本地目录</param>
  81. /// <param name="localFileName">本地文件名</param>
  82. public bool GetFile(string ftpFolder, string ftpFileName, string localFolder, string localFileName)
  83. {
  84. try
  85. {
  86. if (ftp == null) ftp = this.getFtpClient();
  87. if (!ftp.Connected)
  88. {
  89. ftp.Connect();
  90. ftp.ChDir(ftpFolder);
  91. }
  92. ftp.Get(ftpFileName, localFolder, localFileName);
  93. return true;
  94. }
  95. catch
  96. {
  97. try
  98. {
  99. ftp.DisConnect();
  100. ftp = null;
  101. }
  102. catch { ftp = null; }
  103. return false;
  104. }
  105. }
  106. /// <summary>
  107. /// 修改文件
  108. /// </summary>
  109. /// <param name="ftpFolder">本地目录</param>
  110. /// <param name="ftpFileName">本地文件名temp</param>
  111. /// <param name="localFolder">本地目录</param>
  112. /// <param name="localFileName">本地文件名</param>
  113. public bool AddMSCFile(string ftpFolder, string ftpFileName, string localFolder, string localFileName, string BscInfo)
  114. {
  115. string sLine = "";
  116. string sResult = "";
  117. string path = "获得应用程序所在的完整的路径";
  118. path = path.Substring(0, path.LastIndexOf("\\"));
  119. try
  120. {
  121. FileStream fsFile = new FileStream(ftpFolder + "\\" + ftpFileName, FileMode.Open);
  122. FileStream fsFileWrite = new FileStream(localFolder + "\\" + localFileName, FileMode.Create);
  123. StreamReader sr = new StreamReader(fsFile);
  124. StreamWriter sw = new StreamWriter(fsFileWrite);
  125. sr.BaseStream.Seek(0, SeekOrigin.Begin);
  126. while (sr.Peek() > -1)
  127. {
  128. sLine = sr.ReadToEnd();
  129. }
  130. string[] arStr = sLine.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
  131. for (int i = 0; i < arStr.Length - 1; i++)
  132. {
  133. sResult += BscInfo + "," + arStr[i].Trim() + "\n";
  134. }
  135. sr.Close();
  136. byte[] connect = new UTF8Encoding(true).GetBytes(sResult);
  137. fsFileWrite.Write(connect, 0, connect.Length);
  138. fsFileWrite.Flush();
  139. sw.Close();
  140. fsFile.Close();
  141. fsFileWrite.Close();
  142. return true;
  143. }
  144. catch (Exception e)
  145. {
  146. return false;
  147. }
  148. }
  149. /// <summary>
  150. /// 删除文件
  151. /// </summary>
  152. /// <param name="ftpFolder">ftp目录</param>
  153. /// <param name="ftpFileName">ftp文件名</param>
  154. public bool DelFile(string ftpFolder, string ftpFileName)
  155. {
  156. try
  157. {
  158. if (ftp == null) ftp = this.getFtpClient();
  159. if (!ftp.Connected)
  160. {
  161. ftp.Connect();
  162. ftp.ChDir(ftpFolder);
  163. }
  164. ftp.Delete(ftpFileName);
  165. return true;
  166. }
  167. catch
  168. {
  169. return false;
  170. }
  171. }
  172. /// <summary>
  173. /// 上传文件
  174. /// </summary>
  175. /// <param name="ftpFolder">ftp目录</param>
  176. /// <param name="ftpFileName">ftp文件名</param>
  177. public bool PutFile(string ftpFolder, string ftpFileName)
  178. {
  179. try
  180. {
  181. if (ftp == null) ftp = this.getFtpClient();
  182. if (!ftp.Connected)
  183. {
  184. ftp.Connect();
  185. ftp.ChDir(ftpFolder);
  186. }
  187. ftp.Put(ftpFileName);
  188. return true;
  189. }
  190. catch
  191. {
  192. return false;
  193. }
  194. }
  195. /// <summary>
  196. /// 下载文件
  197. /// </summary>
  198. /// <param name="ftpFolder">ftp目录</param>
  199. /// <param name="ftpFileName">ftp文件名</param>
  200. /// <param name="localFolder">本地目录</param>
  201. /// <param name="localFileName">本地文件名</param>
  202. public bool GetFileNoBinary(string ftpFolder, string ftpFileName, string localFolder, string localFileName)
  203. {
  204. try
  205. {
  206. if (ftp == null) ftp = this.getFtpClient();
  207. if (!ftp.Connected)
  208. {
  209. ftp.Connect();
  210. ftp.ChDir(ftpFolder);
  211. }
  212. ftp.GetNoBinary(ftpFileName, localFolder, localFileName);
  213. return true;
  214. }
  215. catch
  216. {
  217. try
  218. {
  219. ftp.DisConnect();
  220. ftp = null;
  221. }
  222. catch
  223. {
  224. ftp = null;
  225. }
  226. return false;
  227. }
  228. }
  229. /// <summary>
  230. /// 得到FTP上文件信息
  231. /// </summary>
  232. /// <param name="ftpFolder">FTP目录</param>
  233. /// <param name="ftpFileName">ftp文件名</param>
  234. public string GetFileInfo(string ftpFolder, string ftpFileName)
  235. {
  236. string strResult = "";
  237. try
  238. {
  239. if (ftp == null) ftp = this.getFtpClient();
  240. if (ftp.Connected) ftp.DisConnect();
  241. ftp.Connect();
  242. ftp.ChDir(ftpFolder);
  243. strResult = ftp.GetFileInfo(ftpFileName);
  244. return strResult;
  245. }
  246. catch
  247. {
  248. return "";
  249. }
  250. }
  251. /// <summary>
  252. /// 测试FTP服务器是否可登陆
  253. /// </summary>
  254. public bool CanConnect()
  255. {
  256. if (ftp == null) ftp = this.getFtpClient();
  257. try
  258. {
  259. ftp.Connect();
  260. ftp.DisConnect();
  261. return true;
  262. }
  263. catch
  264. {
  265. return false;
  266. }
  267. }
  268. /// <summary>
  269. /// 得到FTP上文件信息
  270. /// </summary>
  271. /// <param name="ftpFolder">FTP目录</param>
  272. /// <param name="ftpFileName">ftp文件名</param>
  273. public string GetFileInfoConnected(string ftpFolder, string ftpFileName)
  274. {
  275. string strResult = "";
  276. try
  277. {
  278. if (ftp == null) ftp = this.getFtpClient();
  279. if (!ftp.Connected)
  280. {
  281. ftp.Connect();
  282. ftp.ChDir(ftpFolder);
  283. }
  284. strResult = ftp.GetFileInfo(ftpFileName);
  285. return strResult;
  286. }
  287. catch
  288. {
  289. return "";
  290. }
  291. }
  292. /// <summary>
  293. /// 得到文件列表
  294. /// </summary>
  295. /// <param name="ftpFolder">FTP目录</param>
  296. /// <returns>FTP通配符号</returns>
  297. public string[] GetFileList(string ftpFolder, string strMask)
  298. {
  299. string[] strResult;
  300. try
  301. {
  302. if (ftp == null) ftp = this.getFtpClient();
  303. if (!ftp.Connected)
  304. {
  305. ftp.Connect();
  306. ftp.ChDir(ftpFolder);
  307. }
  308. strResult = ftp.Dir(strMask);
  309. return strResult;
  310. }
  311. catch
  312. {
  313. return null;
  314. }
  315. }
  316. /// <summary>
  317. ///得到FTP传输对象
  318. /// </summary>
  319. public FTPClient getFtpClient()
  320. {
  321. FTPClient ft = new FTPClient();
  322. ft.RemoteHost = this.Server;
  323. ft.RemoteUser = this.User;
  324. ft.RemotePass = this.Pass;
  325. return ft;
  326. }
  327. }
  328. }