Guest User

Untitled

a guest
Mar 28th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1. public string[] GetFileList()
  2. {
  3. string[] downloadFiles;
  4. StringBuilder result = new StringBuilder();
  5. WebResponse response = null;
  6. StreamReader reader = null;
  7.  
  8. try
  9. {
  10. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
  11. request.UseBinary = true;
  12. request.Method = WebRequestMethods.Ftp.ListDirectory;
  13. request.Credentials = new NetworkCredential(ftpUserName, ftpPassWord);
  14. request.KeepAlive = false;
  15. request.UsePassive = false;
  16. response = request.GetResponse();
  17. reader = new StreamReader(response.GetResponseStream());
  18. string line = reader.ReadLine();
  19. while (line != null)
  20. {
  21. result.Append(line);
  22. result.Append("n");
  23. line = reader.ReadLine();
  24. }
  25. result.Remove(result.ToString().LastIndexOf('n'), 1);
  26. return result.ToString().Split('n');
  27. }
  28. catch (Exception ex)
  29. {
  30. if (reader != null)
  31. {
  32. reader.Close();
  33. }
  34. if (response != null)
  35. {
  36. response.Close();
  37. }
  38. downloadFiles = null;
  39. return downloadFiles;
  40. }
  41. }
  42.  
  43. private void Download(string file)
  44. {
  45. try
  46. {
  47. string uri = url + "/" + file;
  48. Uri serverUri = new Uri(uri);
  49. if (serverUri.Scheme != Uri.UriSchemeFtp)
  50. {
  51. return;
  52. }
  53. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url + "/" + file);
  54. request.UseBinary = true;
  55. request.Method = WebRequestMethods.Ftp.DownloadFile;
  56. request.Credentials = new NetworkCredential(ftpUserName, ftpPassWord);
  57. request.KeepAlive = false;
  58. request.UsePassive = false;
  59. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  60. Stream responseStream = response.GetResponseStream();
  61. FileStream writeStream = new FileStream(localDestnDir + "\" + file, FileMode.Create);
  62. int Length = 2048;
  63. Byte[] buffer = new Byte[Length];
  64. int bytesRead = responseStream.Read(buffer, 0, Length);
  65. while (bytesRead > 0)
  66. {
  67. writeStream.Write(buffer, 0, bytesRead);
  68. bytesRead = responseStream.Read(buffer, 0, Length);
  69. }
  70. writeStream.Close();
  71. response.Close();
  72. }
  73. catch (WebException wEx)
  74. {
  75. MessageBox.Show(wEx.Message, "Download Error");
  76. }
  77. catch (Exception ex)
  78. {
  79. MessageBox.Show(ex.Message, "Download Error");
  80. }
  81. }
  82.  
  83. void DownloadFtpDirectory(string url, NetworkCredential credentials, string localPath)
  84. {
  85. FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(url);
  86. listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
  87. listRequest.Credentials = credentials;
  88.  
  89. List<string> lines = new List<string>();
  90.  
  91. using (FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse())
  92. using (Stream listStream = listResponse.GetResponseStream())
  93. using (StreamReader listReader = new StreamReader(listStream))
  94. {
  95. while (!listReader.EndOfStream)
  96. {
  97. lines.Add(listReader.ReadLine());
  98. }
  99. }
  100.  
  101. foreach (string line in lines)
  102. {
  103. string[] tokens =
  104. line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
  105. string name = tokens[8];
  106. string permissions = tokens[0];
  107.  
  108. string localFilePath = Path.Combine(localPath, name);
  109. string fileUrl = url + name;
  110.  
  111. if (permissions[0] == 'd')
  112. {
  113. if (!Directory.Exists(localFilePath))
  114. {
  115. Directory.CreateDirectory(localFilePath);
  116. }
  117.  
  118. DownloadFtpDirectory(fileUrl + "/", credentials, localFilePath);
  119. }
  120. else
  121. {
  122. FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(fileUrl);
  123. downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
  124. downloadRequest.Credentials = credentials;
  125.  
  126. using (FtpWebResponse downloadResponse =
  127. (FtpWebResponse)downloadRequest.GetResponse())
  128. using (Stream sourceStream = downloadResponse.GetResponseStream())
  129. using (Stream targetStream = File.Create(localFilePath))
  130. {
  131. byte[] buffer = new byte[10240];
  132. int read;
  133. while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
  134. {
  135. targetStream.Write(buffer, 0, read);
  136. }
  137. }
  138. }
  139. }
  140. }
  141.  
  142. NetworkCredential credentials = new NetworkCredential("user", "mypassword");
  143. string url = "ftp://ftp.example.com/directory/to/download/";
  144. DownloadFtpDirectory(url, credentials, @"C:targetdirectory");
  145.  
  146. // Setup session options
  147. SessionOptions sessionOptions = new SessionOptions
  148. {
  149. Protocol = Protocol.Ftp,
  150. HostName = "ftp.example.com",
  151. UserName = "user",
  152. Password = "mypassword",
  153. };
  154.  
  155. using (Session session = new Session())
  156. {
  157. // Connect
  158. session.Open(sessionOptions);
  159.  
  160. // Download files
  161. session.GetFiles("/directory/to/download/*", @"C:targetdirectory*").Check();
  162. }
Add Comment
Please, Sign In to add comment