Advertisement
Guest User

ftpclient.cs

a guest
Oct 31st, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.35 KB | None | 0 0
  1. using System;
  2.  
  3. using System.Collections.Generic;
  4.  
  5. using System.Linq;
  6.  
  7. using System.Text;
  8.  
  9. using System.IO;
  10.  
  11. using System.Net;
  12.  
  13.  
  14.  
  15. namespace Lib.Common
  16.  
  17. {
  18.  
  19. public class FtpClient
  20.  
  21. {
  22.  
  23. private string host = null;
  24.  
  25. private string user = null;
  26.  
  27. private string pass = null;
  28.  
  29. private FtpWebRequest ftpRequest = null;
  30.  
  31. private FtpWebResponse ftpResponse = null;
  32.  
  33. private Stream ftpStream = null;
  34.  
  35. private int bufferSize = 2048;
  36.  
  37.  
  38.  
  39. /* Construct Object */
  40.  
  41. public FtpClient(string hostIP, string userName, string password)
  42.  
  43. {
  44.  
  45. host = hostIP; user = userName; pass = password;
  46.  
  47. }
  48.  
  49.  
  50.  
  51. /* Download File */
  52.  
  53. public void download(string remoteFile, string localFile)
  54.  
  55. {
  56.  
  57. try
  58.  
  59. {
  60.  
  61. /* Create an FTP Request */
  62.  
  63. ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
  64.  
  65. /* Log in to the FTP Server with the User Name and Password Provided */
  66.  
  67. ftpRequest.Credentials = new NetworkCredential(user, pass);
  68.  
  69. /* When in doubt, use these options */
  70.  
  71. ftpRequest.UseBinary = true;
  72.  
  73. ftpRequest.UsePassive = true;
  74.  
  75. ftpRequest.KeepAlive = true;
  76.  
  77. /* Specify the Type of FTP Request */
  78.  
  79. ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
  80.  
  81. /* Establish Return Communication with the FTP Server */
  82.  
  83. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  84.  
  85. /* Get the FTP Server's Response Stream */
  86.  
  87. ftpStream = ftpResponse.GetResponseStream();
  88.  
  89. /* Open a File Stream to Write the Downloaded File */
  90.  
  91. FileStream localFileStream = new FileStream(localFile, FileMode.Create);
  92.  
  93. /* Buffer for the Downloaded Data */
  94.  
  95. byte[] byteBuffer = new byte[bufferSize];
  96.  
  97. int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
  98.  
  99. /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
  100.  
  101. try
  102.  
  103. {
  104.  
  105. while (bytesRead > 0)
  106.  
  107. {
  108.  
  109. localFileStream.Write(byteBuffer, 0, bytesRead);
  110.  
  111. bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
  112.  
  113. }
  114.  
  115. }
  116.  
  117. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  118.  
  119. /* Resource Cleanup */
  120.  
  121. localFileStream.Close();
  122.  
  123. ftpStream.Close();
  124.  
  125. ftpResponse.Close();
  126.  
  127. ftpRequest = null;
  128.  
  129. }
  130.  
  131. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  132.  
  133. return;
  134.  
  135. }
  136.  
  137.  
  138.  
  139. /* Upload File */
  140.  
  141. public void upload(string remoteFile, string localFile)
  142.  
  143. {
  144.  
  145. try
  146.  
  147. {
  148.  
  149. /* Create an FTP Request */
  150.  
  151. ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
  152.  
  153. /* Log in to the FTP Server with the User Name and Password Provided */
  154.  
  155. ftpRequest.Credentials = new NetworkCredential(user, pass);
  156.  
  157. /* When in doubt, use these options */
  158.  
  159. ftpRequest.UseBinary = true;
  160.  
  161. ftpRequest.UsePassive = true;
  162.  
  163. ftpRequest.KeepAlive = true;
  164.  
  165. /* Specify the Type of FTP Request */
  166.  
  167. ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
  168.  
  169. /* Establish Return Communication with the FTP Server */
  170.  
  171. ftpStream = ftpRequest.GetRequestStream();
  172.  
  173. /* Open a File Stream to Read the File for Upload */
  174.  
  175. FileStream localFileStream = new FileStream(localFile, FileMode.Create);
  176.  
  177. /* Buffer for the Downloaded Data */
  178.  
  179. byte[] byteBuffer = new byte[bufferSize];
  180.  
  181. int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
  182.  
  183. /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
  184.  
  185. try
  186.  
  187. {
  188.  
  189. while (bytesSent != 0)
  190.  
  191. {
  192.  
  193. ftpStream.Write(byteBuffer, 0, bytesSent);
  194.  
  195. bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
  196.  
  197. }
  198.  
  199. }
  200.  
  201. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  202.  
  203. /* Resource Cleanup */
  204.  
  205. localFileStream.Close();
  206.  
  207. ftpStream.Close();
  208.  
  209. ftpRequest = null;
  210.  
  211. }
  212.  
  213. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  214.  
  215. return;
  216.  
  217. }
  218.  
  219.  
  220.  
  221. /* Delete File */
  222.  
  223. public void delete(string deleteFile)
  224.  
  225. {
  226.  
  227. try
  228.  
  229. {
  230.  
  231. /* Create an FTP Request */
  232.  
  233. ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile);
  234.  
  235. /* Log in to the FTP Server with the User Name and Password Provided */
  236.  
  237. ftpRequest.Credentials = new NetworkCredential(user, pass);
  238.  
  239. /* When in doubt, use these options */
  240.  
  241. ftpRequest.UseBinary = true;
  242.  
  243. ftpRequest.UsePassive = true;
  244.  
  245. ftpRequest.KeepAlive = true;
  246.  
  247. /* Specify the Type of FTP Request */
  248.  
  249. ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
  250.  
  251. /* Establish Return Communication with the FTP Server */
  252.  
  253. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  254.  
  255. /* Resource Cleanup */
  256.  
  257. ftpResponse.Close();
  258.  
  259. ftpRequest = null;
  260.  
  261. }
  262.  
  263. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  264.  
  265. return;
  266.  
  267. }
  268.  
  269. /* Delete Dir*/
  270.  
  271. public void deleteDir(string deleteFile)
  272.  
  273. {
  274.  
  275. try
  276.  
  277. {
  278.  
  279. /* Create an FTP Request */
  280.  
  281. ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile);
  282.  
  283. /* Log in to the FTP Server with the User Name and Password Provided */
  284.  
  285. ftpRequest.Credentials = new NetworkCredential(user, pass);
  286.  
  287. /* When in doubt, use these options */
  288.  
  289. ftpRequest.UseBinary = true;
  290.  
  291. ftpRequest.UsePassive = true;
  292.  
  293. ftpRequest.KeepAlive = true;
  294.  
  295. /* Specify the Type of FTP Request */
  296.  
  297. ftpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
  298.  
  299. /* Establish Return Communication with the FTP Server */
  300.  
  301. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  302.  
  303. /* Resource Cleanup */
  304.  
  305. ftpResponse.Close();
  306.  
  307. ftpRequest = null;
  308.  
  309. }
  310.  
  311. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  312.  
  313. return;
  314.  
  315. }
  316.  
  317. /* Rename File */
  318.  
  319. public void rename(string currentFileNameAndPath, string newFileName)
  320.  
  321. {
  322.  
  323. try
  324.  
  325. {
  326.  
  327. /* Create an FTP Request */
  328.  
  329. ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + currentFileNameAndPath);
  330.  
  331. /* Log in to the FTP Server with the User Name and Password Provided */
  332.  
  333. ftpRequest.Credentials = new NetworkCredential(user, pass);
  334.  
  335. /* When in doubt, use these options */
  336.  
  337. ftpRequest.UseBinary = true;
  338.  
  339. ftpRequest.UsePassive = true;
  340.  
  341. ftpRequest.KeepAlive = true;
  342.  
  343. /* Specify the Type of FTP Request */
  344.  
  345. ftpRequest.Method = WebRequestMethods.Ftp.Rename;
  346.  
  347. /* Rename the File */
  348.  
  349. ftpRequest.RenameTo = newFileName;
  350.  
  351. /* Establish Return Communication with the FTP Server */
  352.  
  353. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  354.  
  355. /* Resource Cleanup */
  356.  
  357. ftpResponse.Close();
  358.  
  359. ftpRequest = null;
  360.  
  361. }
  362.  
  363. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  364.  
  365. return;
  366.  
  367. }
  368.  
  369.  
  370.  
  371. /* Create a New Directory on the FTP Server */
  372.  
  373. public void createDirectory(string newDirectory)
  374.  
  375. {
  376.  
  377. try
  378.  
  379. {
  380.  
  381. /* Create an FTP Request */
  382.  
  383. ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + newDirectory);
  384.  
  385. /* Log in to the FTP Server with the User Name and Password Provided */
  386.  
  387. ftpRequest.Credentials = new NetworkCredential(user, pass);
  388.  
  389. /* When in doubt, use these options */
  390.  
  391. ftpRequest.UseBinary = true;
  392.  
  393. ftpRequest.UsePassive = true;
  394.  
  395. ftpRequest.KeepAlive = true;
  396.  
  397. /* Specify the Type of FTP Request */
  398.  
  399. ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
  400.  
  401. /* Establish Return Communication with the FTP Server */
  402.  
  403. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  404.  
  405. /* Resource Cleanup */
  406.  
  407. ftpResponse.Close();
  408.  
  409. ftpRequest = null;
  410.  
  411. }
  412.  
  413. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  414.  
  415. return;
  416.  
  417. }
  418.  
  419.  
  420.  
  421. /* Get the Date/Time a File was Created */
  422.  
  423. public string getFileCreatedDateTime(string fileName)
  424.  
  425. {
  426.  
  427. try
  428.  
  429. {
  430.  
  431. /* Create an FTP Request */
  432.  
  433. ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
  434.  
  435. /* Log in to the FTP Server with the User Name and Password Provided */
  436.  
  437. ftpRequest.Credentials = new NetworkCredential(user, pass);
  438.  
  439. /* When in doubt, use these options */
  440.  
  441. ftpRequest.UseBinary = true;
  442.  
  443. ftpRequest.UsePassive = true;
  444.  
  445. ftpRequest.KeepAlive = true;
  446.  
  447. /* Specify the Type of FTP Request */
  448.  
  449. ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
  450.  
  451. /* Establish Return Communication with the FTP Server */
  452.  
  453. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  454.  
  455. /* Establish Return Communication with the FTP Server */
  456.  
  457. ftpStream = ftpResponse.GetResponseStream();
  458.  
  459. /* Get the FTP Server's Response Stream */
  460.  
  461. StreamReader ftpReader = new StreamReader(ftpStream);
  462.  
  463. /* Store the Raw Response */
  464.  
  465. string fileInfo = null;
  466.  
  467. /* Read the Full Response Stream */
  468.  
  469. try { fileInfo = ftpReader.ReadToEnd(); }
  470.  
  471. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  472.  
  473. /* Resource Cleanup */
  474.  
  475. ftpReader.Close();
  476.  
  477. ftpStream.Close();
  478.  
  479. ftpResponse.Close();
  480.  
  481. ftpRequest = null;
  482.  
  483. /* Return File Created Date Time */
  484.  
  485. return fileInfo;
  486.  
  487. }
  488.  
  489. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  490.  
  491. /* Return an Empty string Array if an Exception Occurs */
  492.  
  493. return "";
  494.  
  495. }
  496.  
  497.  
  498.  
  499. /* Get the Size of a File */
  500.  
  501. public string getFileSize(string fileName)
  502.  
  503. {
  504.  
  505. try
  506.  
  507. {
  508.  
  509. /* Create an FTP Request */
  510.  
  511. ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
  512.  
  513. /* Log in to the FTP Server with the User Name and Password Provided */
  514.  
  515. ftpRequest.Credentials = new NetworkCredential(user, pass);
  516.  
  517. /* When in doubt, use these options */
  518.  
  519. ftpRequest.UseBinary = true;
  520.  
  521. ftpRequest.UsePassive = true;
  522.  
  523. ftpRequest.KeepAlive = true;
  524.  
  525. /* Specify the Type of FTP Request */
  526.  
  527. ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
  528.  
  529. /* Establish Return Communication with the FTP Server */
  530.  
  531. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  532.  
  533. /* Establish Return Communication with the FTP Server */
  534.  
  535. ftpStream = ftpResponse.GetResponseStream();
  536.  
  537. /* Get the FTP Server's Response Stream */
  538.  
  539. StreamReader ftpReader = new StreamReader(ftpStream);
  540.  
  541. /* Store the Raw Response */
  542.  
  543. string fileInfo = null;
  544.  
  545. /* Read the Full Response Stream */
  546.  
  547. try { while (ftpReader.Peek() != -1) { fileInfo = ftpReader.ReadToEnd(); } }
  548.  
  549. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  550.  
  551. /* Resource Cleanup */
  552.  
  553. ftpReader.Close();
  554.  
  555. ftpStream.Close();
  556.  
  557. ftpResponse.Close();
  558.  
  559. ftpRequest = null;
  560.  
  561. /* Return File Size */
  562.  
  563. return fileInfo;
  564.  
  565. }
  566.  
  567. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  568.  
  569. /* Return an Empty string Array if an Exception Occurs */
  570.  
  571. return "";
  572.  
  573. }
  574.  
  575.  
  576.  
  577. /* List Directory Contents File/Folder Name Only */
  578.  
  579. public string[] directoryListSimple(string directory)
  580.  
  581. {
  582.  
  583. try
  584.  
  585. {
  586.  
  587. /* Create an FTP Request */
  588.  
  589. ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
  590.  
  591. /* Log in to the FTP Server with the User Name and Password Provided */
  592.  
  593. ftpRequest.Credentials = new NetworkCredential(user, pass);
  594.  
  595. /* When in doubt, use these options */
  596.  
  597. ftpRequest.UseBinary = true;
  598.  
  599. ftpRequest.UsePassive = true;
  600.  
  601. ftpRequest.KeepAlive = true;
  602.  
  603. /* Specify the Type of FTP Request */
  604.  
  605. ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
  606.  
  607. /* Establish Return Communication with the FTP Server */
  608.  
  609. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  610.  
  611. /* Establish Return Communication with the FTP Server */
  612.  
  613. ftpStream = ftpResponse.GetResponseStream();
  614.  
  615. /* Get the FTP Server's Response Stream */
  616.  
  617. StreamReader ftpReader = new StreamReader(ftpStream);
  618.  
  619. /* Store the Raw Response */
  620.  
  621. string directoryRaw = null;
  622.  
  623. /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
  624.  
  625. try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } }
  626.  
  627. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  628.  
  629. /* Resource Cleanup */
  630.  
  631. ftpReader.Close();
  632.  
  633. ftpStream.Close();
  634.  
  635. ftpResponse.Close();
  636.  
  637. ftpRequest = null;
  638.  
  639. /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
  640.  
  641. try { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; }
  642.  
  643. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  644.  
  645. }
  646.  
  647. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  648.  
  649. /* Return an Empty string Array if an Exception Occurs */
  650.  
  651. return new string[] { "" };
  652.  
  653. }
  654.  
  655.  
  656.  
  657. /* List Directory Contents in Detail (Name, Size, Created, etc.) */
  658.  
  659. public string[] directoryListDetailed(string directory)
  660.  
  661. {
  662.  
  663. try
  664.  
  665. {
  666.  
  667. /* Create an FTP Request */
  668.  
  669. ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
  670.  
  671. /* Log in to the FTP Server with the User Name and Password Provided */
  672.  
  673. ftpRequest.Credentials = new NetworkCredential(user, pass);
  674.  
  675. /* When in doubt, use these options */
  676.  
  677. ftpRequest.UseBinary = true;
  678.  
  679. ftpRequest.UsePassive = true;
  680.  
  681. ftpRequest.KeepAlive = true;
  682.  
  683. /* Specify the Type of FTP Request */
  684.  
  685. ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  686.  
  687. /* Establish Return Communication with the FTP Server */
  688.  
  689. ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  690.  
  691. /* Establish Return Communication with the FTP Server */
  692.  
  693. ftpStream = ftpResponse.GetResponseStream();
  694.  
  695. /* Get the FTP Server's Response Stream */
  696.  
  697. StreamReader ftpReader = new StreamReader(ftpStream);
  698.  
  699. /* Store the Raw Response */
  700.  
  701. string directoryRaw = null;
  702.  
  703. /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
  704.  
  705. try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } }
  706.  
  707. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  708.  
  709. /* Resource Cleanup */
  710.  
  711. ftpReader.Close();
  712.  
  713. ftpStream.Close();
  714.  
  715. ftpResponse.Close();
  716.  
  717. ftpRequest = null;
  718.  
  719. /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
  720.  
  721. try { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; }
  722.  
  723. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  724.  
  725. }
  726.  
  727. catch (Exception ex) { Console.WriteLine(ex.ToString()); }
  728.  
  729. /* Return an Empty string Array if an Exception Occurs */
  730.  
  731. return new string[] { "" };
  732.  
  733. }
  734.  
  735. }
  736.  
  737. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement