Advertisement
Guest User

Untitled

a guest
May 13th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.24 KB | None | 0 0
  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.Threading.Tasks;
  8.  
  9.  
  10. public class FtpClient
  11. {
  12. private string _host = null;
  13. private string _user = null;
  14. private string _pass = null;
  15. private FtpWebRequest ftpRequest = null;
  16. private FtpWebResponse ftpResponse = null;
  17. private Stream ftpStream = null;
  18. private int bufferSize = 2048;
  19.  
  20. /* Construct Object */
  21.  
  22. public FtpClient(string hostIp, string userName, string password)
  23. {
  24. _host = hostIp;
  25. _user = userName;
  26. _pass = password;
  27. }
  28.  
  29. /* Download File */
  30.  
  31. public void Download(string remoteFile, string localFile)
  32. {
  33. //try
  34. //{
  35. /* Create an FTP Request */
  36. ftpRequest = (FtpWebRequest)FtpWebRequest.Create(_host + "/" + remoteFile);
  37. /* Log in to the FTP Server with the User Name and Password Provided */
  38. ftpRequest.Credentials = new NetworkCredential(_user, _pass);
  39. /* When in doubt, use these options */
  40. ftpRequest.UseBinary = true;
  41. ftpRequest.UsePassive = true;
  42. ftpRequest.KeepAlive = true;
  43. /* Specify the Type of FTP Request */
  44. ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
  45. /* Establish Return Communication with the FTP Server */
  46. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  47. /* Get the FTP Server's Response Stream */
  48. ftpStream = ftpResponse.GetResponseStream();
  49. /* Open a File Stream to Write the Downloaded File */
  50. FileStream localFileStream = new FileStream(localFile, FileMode.Create);
  51. /* Buffer for the Downloaded Data */
  52. byte[] byteBuffer = new byte[bufferSize];
  53. int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
  54. /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
  55. try
  56. {
  57. while (bytesRead > 0)
  58. {
  59. localFileStream.Write(byteBuffer, 0, bytesRead);
  60. bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
  61. }
  62. }
  63. catch (Exception ex)
  64. {
  65. Console.WriteLine(ex.ToString());
  66. }
  67. /* Resource Cleanup */
  68. localFileStream.Close();
  69. ftpStream.Close();
  70. ftpResponse.Close();
  71. ftpRequest = null;
  72. ftpResponse = null;
  73. ftpStream = null;
  74. //}
  75. //catch (Exception ex)
  76. //{
  77. // Console.WriteLine(ex.ToString());
  78. //}
  79. //return;
  80. }
  81.  
  82. /* Upload File */
  83.  
  84. public void Upload(string remoteFile, string localFile)
  85. {
  86. //try
  87. //{
  88. /* Create an FTP Request */
  89. ftpRequest = (FtpWebRequest)FtpWebRequest.Create(_host + remoteFile);
  90. /* Log in to the FTP Server with the User Name and Password Provided */
  91. ftpRequest.Credentials = new NetworkCredential(_user, _pass);
  92. /* When in doubt, use these options */
  93. ftpRequest.UseBinary = true;
  94. ftpRequest.UsePassive = true;
  95. ftpRequest.KeepAlive = true;
  96. /* Specify the Type of FTP Request */
  97. ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
  98. /* Establish Return Communication with the FTP Server */
  99. ftpStream = ftpRequest.GetRequestStream();
  100. /* Open a File Stream to Read the File for Upload */
  101. FileStream localFileStream = new FileStream(localFile, FileMode.Open);
  102. /* Buffer for the Downloaded Data */
  103. byte[] byteBuffer = new byte[bufferSize];
  104. int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
  105. /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
  106. try
  107. {
  108. while (bytesSent != 0)
  109. {
  110. ftpStream.Write(byteBuffer, 0, bytesSent);
  111. bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. Console.WriteLine(ex.ToString());
  117. }
  118. /* Resource Cleanup */
  119. localFileStream.Close();
  120. ftpStream.Close();
  121. ftpRequest = null;
  122. ftpStream = null;
  123. //}
  124. //catch (Exception ex)
  125. //{
  126. // Console.WriteLine(ex.ToString());
  127. //}
  128. //return;
  129. }
  130.  
  131. /* Delete File */
  132.  
  133. public void Delete(string deleteFile)
  134. {
  135. try
  136. {
  137. /* Create an FTP Request */
  138. ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + deleteFile);
  139. /* Log in to the FTP Server with the User Name and Password Provided */
  140. ftpRequest.Credentials = new NetworkCredential(_user, _pass);
  141. /* When in doubt, use these options */
  142. ftpRequest.UseBinary = true;
  143. ftpRequest.UsePassive = true;
  144. ftpRequest.KeepAlive = true;
  145. /* Specify the Type of FTP Request */
  146. ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
  147. /* Establish Return Communication with the FTP Server */
  148. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  149. /* Resource Cleanup */
  150. ftpResponse.Close();
  151. ftpRequest = null;
  152. ftpResponse = null;
  153. }
  154. catch (Exception ex)
  155. {
  156. Console.WriteLine(ex.ToString());
  157. }
  158. return;
  159. }
  160.  
  161. /* Rename File */
  162.  
  163. public void Rename(string currentFileNameAndPath, string newFileName)
  164. {
  165. //try
  166. //{
  167. /* Create an FTP Request */
  168. ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + currentFileNameAndPath);
  169. /* Log in to the FTP Server with the User Name and Password Provided */
  170. ftpRequest.Credentials = new NetworkCredential(_user, _pass);
  171. /* When in doubt, use these options */
  172. ftpRequest.UseBinary = true;
  173. ftpRequest.UsePassive = true;
  174. ftpRequest.KeepAlive = true;
  175. /* Specify the Type of FTP Request */
  176. ftpRequest.Method = WebRequestMethods.Ftp.Rename;
  177. /* Rename the File */
  178. ftpRequest.RenameTo = newFileName;
  179. /* Establish Return Communication with the FTP Server */
  180. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  181. /* Resource Cleanup */
  182. ftpResponse.Close();
  183. ftpRequest = null;
  184. ftpResponse = null;
  185. //}
  186. //catch (Exception ex)
  187. //{
  188. // Console.WriteLine(ex.ToString());
  189. //}
  190. //return;
  191. }
  192.  
  193. /* Create a New Directory on the FTP Server */
  194.  
  195. public void CreateDirectory(string newDirectory)
  196. {
  197. //try
  198. //{
  199. /* Create an FTP Request */
  200. ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + newDirectory);
  201. /* Log in to the FTP Server with the User Name and Password Provided */
  202. ftpRequest.Credentials = new NetworkCredential(_user, _pass);
  203. /* When in doubt, use these options */
  204. ftpRequest.UseBinary = true;
  205. ftpRequest.UsePassive = true;
  206. ftpRequest.KeepAlive = true;
  207. /* Specify the Type of FTP Request */
  208. ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
  209. /* Establish Return Communication with the FTP Server */
  210. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  211. /* Resource Cleanup */
  212. ftpResponse.Close();
  213. ftpRequest = null;
  214. ftpResponse = null;
  215. //}
  216. //catch (Exception ex)
  217. //{
  218. // Console.WriteLine(ex.ToString());
  219. //}
  220. //return;
  221. }
  222.  
  223. /* Create a New Directory on the FTP Server */
  224.  
  225. public void RemoveDirectory(string directoryName)
  226. {
  227. //try
  228. //{
  229. /* Create an FTP Request */
  230. ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + directoryName);
  231. /* Log in to the FTP Server with the User Name and Password Provided */
  232. ftpRequest.Credentials = new NetworkCredential(_user, _pass);
  233. /* When in doubt, use these options */
  234. ftpRequest.UseBinary = true;
  235. ftpRequest.UsePassive = true;
  236. ftpRequest.KeepAlive = true;
  237. /* Specify the Type of FTP Request */
  238. ftpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
  239. /* Establish Return Communication with the FTP Server */
  240. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  241. /* Resource Cleanup */
  242. ftpResponse.Close();
  243. ftpRequest = null;
  244. ftpResponse = null;
  245. //}
  246. //catch (Exception ex)
  247. //{
  248. // Console.WriteLine(ex.ToString());
  249. //}
  250. //return;
  251. }
  252.  
  253.  
  254. /* Get the Date/Time a File was Created */
  255.  
  256. public string GetFileCreatedDateTime(string fileName)
  257. {
  258.  
  259. //try
  260. //{
  261. /* Create an FTP Request */
  262. ftpRequest = (FtpWebRequest)FtpWebRequest.Create(_host + "/" + fileName);
  263. /* Log in to the FTP Server with the User Name and Password Provided */
  264. ftpRequest.Credentials = new NetworkCredential(_user, _pass);
  265. /* When in doubt, use these options */
  266. ftpRequest.UseBinary = true;
  267. ftpRequest.UsePassive = true;
  268. ftpRequest.KeepAlive = true;
  269. /* Specify the Type of FTP Request */
  270. ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
  271.  
  272. DateTime fileInfo = ((FtpWebResponse)ftpRequest.GetResponse()).LastModified;
  273.  
  274. /////////////////////////////////
  275. /* comment by arian 9 seot 2015*/
  276. /////////////////////////////////
  277. /* Establish Return Communication with the FTP Server */
  278. //ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  279. /* Establish Return Communication with the FTP Server */
  280. //ftpStream = ftpResponse.GetResponseStream();
  281. /* Get the FTP Server's Response Stream */
  282. //StreamReader ftpReader = new StreamReader(ftpStream);
  283. ///* Store the Raw Response */
  284. //string fileInfo = null;
  285. ///* Read the Full Response Stream */
  286. //try
  287. //{
  288. // fileInfo = ftpReader.ReadToEnd();
  289. //}
  290. //catch (Exception ex)
  291. //{
  292. // Console.WriteLine(ex.ToString());
  293. //}
  294. ///* Resource Cleanup */
  295. //ftpReader.Close();
  296. /////////////////////////////////
  297. /* comment by arian 9 seot 2015*/
  298. /////////////////////////////////
  299.  
  300. // ftpStream.Close();
  301. // ftpResponse.Close();
  302. ftpRequest = null;
  303. // ftpResponse = null;
  304. //ftpStream = null;
  305. /* Return File Created Date Time */
  306. return fileInfo.ToString();
  307. //}
  308. //catch (Exception ex)
  309. //{
  310. // Console.WriteLine(ex.ToString());
  311. //}
  312. /* Return an Empty string Array if an Exception Occurs */
  313. //return "";
  314. }
  315.  
  316. /* Get the Size of a File */
  317.  
  318. public string GetFileSize(string fileName)
  319. {
  320. //try
  321. //{
  322. /* Create an FTP Request */
  323. ftpRequest = (FtpWebRequest)FtpWebRequest.Create(_host + "/" + fileName);
  324. /* Log in to the FTP Server with the User Name and Password Provided */
  325. ftpRequest.Credentials = new NetworkCredential(_user, _pass);
  326. /* When in doubt, use these options */
  327. ftpRequest.UseBinary = true;
  328. ftpRequest.UsePassive = true;
  329. ftpRequest.KeepAlive = true;
  330. /* Specify the Type of FTP Request */
  331. ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
  332.  
  333. var fileInfo = ((FtpWebResponse)ftpRequest.GetResponse()).ContentLength;
  334.  
  335. /////////////////////////////////
  336. /* comment by arian 9 seot 2015*/
  337. /////////////////////////////////
  338. /* Establish Return Communication with the FTP Server */
  339. //ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  340. /* Establish Return Communication with the FTP Server */
  341. //ftpStream = ftpResponse.GetResponseStream();
  342. /* Get the FTP Server's Response Stream */
  343. //StreamReader ftpReader = new StreamReader(ftpStream);
  344. ///* Store the Raw Response */
  345. //string fileInfo = null;
  346. ///* Read the Full Response Stream */
  347. //try
  348. //{
  349. // while (ftpReader.Peek() != -1)
  350. // {
  351. // fileInfo = ftpReader.ReadToEnd();
  352. // }
  353. //}
  354. //catch (Exception ex)
  355. //{
  356. // Console.WriteLine(ex.ToString());
  357. //}
  358. ///* Resource Cleanup */
  359. //ftpReader.Close();
  360. /////////////////////////////////
  361. /* comment by arian 9 seot 2015*/
  362. /////////////////////////////////
  363.  
  364.  
  365.  
  366. //ftpStream.Close();
  367. //ftpResponse.Close();
  368. ftpRequest = null;
  369. // ftpResponse = null;
  370. //ftpStream = null;
  371. /* Return File Size */
  372. return fileInfo.ToString();
  373. //}
  374. //catch (Exception ex)
  375. //{
  376. // Console.WriteLine(ex.ToString());
  377. //}
  378. /* Return an Empty string Array if an Exception Occurs */
  379. //return "";
  380. }
  381.  
  382. /* List Directory Contents File/Folder Name Only */
  383.  
  384. public string[] DirectoryListSimple(string directory)
  385. {
  386. //try
  387. //{
  388. /* Create an FTP Request */
  389. ftpRequest = (FtpWebRequest)FtpWebRequest.Create(_host + "/" + directory);
  390. /* Log in to the FTP Server with the User Name and Password Provided */
  391. ftpRequest.Credentials = new NetworkCredential(_user, _pass);
  392. /* When in doubt, use these options */
  393. ftpRequest.UseBinary = true;
  394. ftpRequest.UsePassive = true;
  395. ftpRequest.KeepAlive = true;
  396. /* Specify the Type of FTP Request */
  397. ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
  398. /* Establish Return Communication with the FTP Server */
  399. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  400. /* Establish Return Communication with the FTP Server */
  401. ftpStream = ftpResponse.GetResponseStream();
  402. /* Get the FTP Server's Response Stream */
  403. StreamReader ftpReader = new StreamReader(ftpStream);
  404. /* Store the Raw Response */
  405. string directoryRaw = null;
  406. /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
  407. try
  408. {
  409. while (ftpReader.Peek() != -1)
  410. {
  411. directoryRaw += ftpReader.ReadLine() + "|";
  412. }
  413. }
  414. catch (Exception ex)
  415. {
  416. Console.WriteLine(ex.ToString());
  417. }
  418. /* Resource Cleanup */
  419. ftpReader.Close();
  420. ftpStream.Close();
  421. ftpResponse.Close();
  422. ftpRequest = null;
  423. ftpResponse = null;
  424. ftpStream = null;
  425. /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
  426. try
  427. {
  428. string[] directoryList = directoryRaw.Split("|".ToCharArray());
  429. return directoryList;
  430. }
  431. catch (Exception ex)
  432. {
  433. Console.WriteLine(ex.ToString());
  434. }
  435. //}
  436. //catch (Exception ex)
  437. //{
  438. // Console.WriteLine(ex.ToString());
  439. //}
  440. ///* Return an Empty string Array if an Exception Occurs */
  441. return new string[] { "" };
  442. }
  443.  
  444. /* List Directory Contents in Detail (Name, Size, Created, etc.) */
  445.  
  446. public string[] DirectoryListDetailed(string directory)
  447. {
  448. //try
  449. //{
  450. /* Create an FTP Request */
  451. ftpRequest = (FtpWebRequest)FtpWebRequest.Create(_host + "/" + directory);
  452. /* Log in to the FTP Server with the User Name and Password Provided */
  453. ftpRequest.Credentials = new NetworkCredential(_user, _pass);
  454. /* When in doubt, use these options */
  455. ftpRequest.UseBinary = true;
  456. ftpRequest.UsePassive = true;
  457. ftpRequest.KeepAlive = true;
  458. /* Specify the Type of FTP Request */
  459. ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  460. /* Establish Return Communication with the FTP Server */
  461. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  462. /* Establish Return Communication with the FTP Server */
  463. ftpStream = ftpResponse.GetResponseStream();
  464. /* Get the FTP Server's Response Stream */
  465. StreamReader ftpReader = new StreamReader(ftpStream);
  466. /* Store the Raw Response */
  467. string directoryRaw = null;
  468. /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
  469. try
  470. {
  471. while (ftpReader.Peek() != -1)
  472. {
  473. directoryRaw += ftpReader.ReadLine() + "|";
  474. }
  475. }
  476. catch (Exception ex)
  477. {
  478. Console.WriteLine(ex.ToString());
  479. }
  480. /* Resource Cleanup */
  481. ftpReader.Close();
  482. ftpStream.Close();
  483. ftpResponse.Close();
  484. ftpRequest = null;
  485. ftpResponse = null;
  486. ftpStream = null;
  487. /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
  488. try
  489. {
  490. string[] directoryList = directoryRaw.Split("|".ToCharArray());
  491. return directoryList;
  492. }
  493. catch (Exception ex)
  494. {
  495. Console.WriteLine(ex.ToString());
  496. }
  497. //}
  498. //catch (Exception ex)
  499. //{
  500. // Console.WriteLine(ex.ToString());
  501. //}
  502. ///* Return an Empty string Array if an Exception Occurs */
  503. return new string[] { "" };
  504. }
  505. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement