Advertisement
Guest User

Untitled

a guest
Sep 12th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using Newtonsoft.Json;
  5.  
  6. namespace Repository
  7. {
  8. /// <summary>
  9. /// ToFtp is used to upload a local file to remote FTP.
  10. /// </summary>
  11. class ToFtp
  12. {
  13.  
  14. /// <summary>
  15. /// Path to FTP configuration files
  16. /// </summary>
  17. private string _configurationPath = @"C:\Users\Public\Documents";
  18. private Credentials _creds = new Credentials();
  19.  
  20. /// <summary>
  21. /// Initializer with hostname/ip, username and password as parameters
  22. /// </summary>
  23. /// <param name="ftpHost">hostname or IP, use "ftp://" as prefix</param>
  24. /// <param name="user">ftp user</param>
  25. /// <param name="pwd">ftp password</param>
  26. public ToFtp(string ftpHost, string user, string pwd)
  27. {
  28. _creds.FtpHost = ftpHost;
  29. _creds.UserName = user;
  30. _creds.PassWord = pwd;
  31. }
  32.  
  33. /// <summary>
  34. /// Initializer which reads the FTP credentials from configuration file.
  35. /// </summary>
  36. /// <param name="fileName"></param>
  37. public ToFtp(string fileName)
  38. {
  39. ReadConfig(Path.Combine(_configurationPath, fileName));
  40. }
  41.  
  42.  
  43. public struct Credentials
  44. {
  45. public string FtpHost;
  46. public string UserName;
  47. public string PassWord;
  48. }
  49.  
  50. /// <summary>
  51. /// Method for upload of local file to remote FTP
  52. /// </summary>
  53. /// <param name="path">Local path</param>
  54. /// <param name="filename">Local filename</param>
  55. public void Upload(string path, string filename)
  56. {
  57. string fileToUpload = Path.Combine(path, filename);
  58. try
  59.  
  60. {
  61. var ftpWebRequest = (FtpWebRequest)WebRequest.Create(_creds.FtpHost + filename);
  62. ftpWebRequest.Credentials = new NetworkCredential(_creds.UserName, _creds.PassWord);
  63. ftpWebRequest.UseBinary = true;
  64. ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
  65. ftpWebRequest.Proxy = null;
  66.  
  67. FileStream fs = File.OpenRead(fileToUpload);
  68. byte[] buffer = new byte[fs.Length];
  69. fs.Read(buffer, 0, buffer.Length);
  70. fs.Close();
  71.  
  72. Stream ftpStream = ftpWebRequest.GetRequestStream();
  73. ftpStream.Write(buffer, 0, buffer.Length);
  74. ftpStream.Close();
  75.  
  76. var ftpWebResponse = (FtpWebResponse) ftpWebRequest.GetResponse();
  77. Console.WriteLine(ftpWebResponse.StatusDescription);
  78.  
  79. }
  80. catch (WebException e)
  81. {
  82. Console.WriteLine(e);
  83. }
  84. }
  85.  
  86. /// <summary>
  87. /// This method will save the FTP credentials, serialized as Json
  88. /// </summary>
  89. /// <param name="fileName">Filename to save FTP credentials as</param>
  90. /// <returns>bool</returns>
  91. public bool SaveConfig(string fileName)
  92. {
  93. try
  94. {
  95. File.Delete(Path.Combine(_configurationPath, fileName));
  96. File.WriteAllText(Path.Combine(_configurationPath, fileName), JsonConvert.SerializeObject(_creds, Formatting.Indented));
  97. return true;
  98. }
  99. catch
  100. {
  101. return false;
  102. }
  103.  
  104. }
  105.  
  106. /// <summary>
  107. /// This method is used when ToFTP is initialized to read FTP credentials from file
  108. /// </summary>
  109. /// <param name="path">This is the full path and filename to configuration file</param>
  110. /// <returns>bool</returns>
  111. private bool ReadConfig(string path)
  112. {
  113. try
  114. {
  115. _creds = JsonConvert.DeserializeObject<Credentials>(File.ReadAllText(path));
  116. return true;
  117. }
  118. catch
  119. {
  120. return false;
  121. }
  122. }
  123.  
  124. }
  125.  
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement