SimpleFTP Class


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /*
  2.  * FileName: SimpleFTP.cs
  3.  * Classes Included: SimpleFTP
  4.  * Purpose: The SimpleFTP is a class that allows for the FTP transfer of remote
  5.  * data to the local hard drive. It also provides three delegates to report
  6.  * progress to the calling method.
  7.  * Created on: 09 Dec 2008
  8.  * Created by: wraith808
  9.  *
  10.  * History:
  11.  * Date Who Note
  12.  * ---------------------------------------------------------------------------
  13.  * 12-09-2008 wraith808 Created
  14.  *
  15.  */
  16.  
  17. using System;
  18. using System.IO;
  19. using System.Net;
  20.  
  21. namespace UtilsLib
  22. {
  23. public delegate void OnFTPFinish(bool ftpSuccessful, string message);
  24. public delegate void OnFTPStart(string message);
  25. public delegate void OnFTPProgress(int bytesTransferred, string message);
  26.  
  27. public class SimpleFTP
  28. {
  29. private bool _UseBinaryMode = true;
  30. public bool UseBinaryMode
  31. {
  32. get { return _UseBinaryMode; }
  33. set { _UseBinaryMode = value; }
  34. }
  35.  
  36. string _LocalFilePath;
  37. public string LocalFilePath
  38. {
  39. get { return _LocalFilePath; }
  40. set { _LocalFilePath = value; }
  41. }
  42.  
  43. string _LocalFileName;
  44. public string LocalFileName
  45. {
  46. get { return _LocalFileName; }
  47. set { _LocalFileName = value; }
  48. }
  49.  
  50. string _URI;
  51. public string URI
  52. {
  53. get { return _URI; }
  54. set { _URI = value; }
  55. }
  56.  
  57. string _UserId;
  58. public string UserId
  59. {
  60. get { return _UserId; }
  61. set { _UserId = value; }
  62. }
  63.  
  64. string _Password;
  65. public string Password
  66. {
  67. get { return _Password; }
  68. set { _Password = value; }
  69. }
  70.  
  71. string _RemoteFileName;
  72. public string RemoteFileName
  73. {
  74. get { return _RemoteFileName; }
  75. set { _RemoteFileName = value; }
  76. }
  77.  
  78. public bool InitiateTransfer(OnFTPProgress OnProgress, OnFTPStart OnStart, OnFTPFinish OnFinish)
  79. {
  80. bool transferResult = true;
  81. if ((_LocalFileName.Length == 0) || (_LocalFilePath.Length == 0) ||
  82. (_UserId.Length == 0) || (_Password.Length == 0) || (_URI.Length == 0) ||
  83. (_RemoteFileName.Length == 0))
  84. {
  85. OnFinish(false, "All parameters not initialized. Transfer aborted.");
  86. transferResult = false;
  87. }
  88. else
  89. {
  90. var outputStream = new FileStream(_LocalFilePath + "\\" + _LocalFileName, FileMode.Create);
  91. var reqFTP = (FtpWebRequest)WebRequest.Create(_URI + "//'" + _RemoteFileName + "'");
  92. try
  93. {
  94. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
  95. reqFTP.UseBinary = _UseBinaryMode;
  96. reqFTP.Credentials = new NetworkCredential(_UserId, _Password);
  97. var response = (FtpWebResponse)reqFTP.GetResponse();
  98. Stream ftpStream = response.GetResponseStream();
  99. long cl = response.ContentLength;
  100. long transferLength = 0;
  101. OnStart(string.Format("Preparing to transfer {0} bytes", cl));
  102. const int bufferSize = 2048;
  103. var buffer = new byte[bufferSize];
  104.  
  105. int readCount = ftpStream.Read(buffer, 0, bufferSize);
  106. while (readCount > 0)
  107. {
  108. outputStream.Write(buffer, 0, readCount);
  109. OnProgress(readCount, string.Format("Transferred {0} bytes.", readCount));
  110. transferLength += readCount;
  111. readCount = ftpStream.Read(buffer, 0, bufferSize);
  112. }
  113. OnFinish(true, string.Format("{0} bytes transferred.", transferLength));
  114. ftpStream.Close();
  115. outputStream.Close();
  116. response.Close();
  117. }
  118. catch (Exception e)
  119. {
  120. OnFinish(false, string.Format("Failed: {0}", e.Message));
  121. transferResult = false;
  122. }
  123. }
  124. return transferResult;
  125. }
  126. }
  127. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.