FairbanksSftpClient.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using NLog;
  6. using Renci.SshNet;
  7. using Renci.SshNet.Common;
  8. namespace Dfs.WayneChina.FairbanksRTData
  9. {
  10. /// <summary>
  11. /// The SFTP client is implemented using SSH.NET 2016.1.0
  12. /// </summary>
  13. public class FairbanksSftpClient
  14. {
  15. #region Properties
  16. public string Host { get; private set; }
  17. public int Port { get; private set; }
  18. public string UserName { get; set; }
  19. public string Password { get; set; }
  20. public SftpClient Client { get; private set; }
  21. public bool Connected { get; set; } = false;
  22. #endregion
  23. #region Logger
  24. NLog.Logger logger = NLog.LogManager.LoadConfiguration("NLog.config").GetLogger("Fairbanks");
  25. #endregion
  26. #region Constructor
  27. public FairbanksSftpClient(string host, string username, string password)
  28. {
  29. RefreshConfig(host, username, password);
  30. }
  31. #endregion
  32. #region Refresh Config
  33. public void RefreshConfig(string host, string username, string password)
  34. {
  35. Host = host;
  36. UserName = username;
  37. Password = password;
  38. //Reset the connection
  39. Client = null;
  40. Connected = false;
  41. }
  42. #endregion
  43. #region Initialize
  44. public void Initialize()
  45. {
  46. if (Client != null)
  47. return;
  48. logger.Info($"Initializing SFTP client against: {Host}, {UserName}, {Password}");
  49. Client = new SftpClient(Host, 22, UserName, Password);
  50. }
  51. #endregion
  52. #region Connection (connect and disconnect)
  53. public void Connect()
  54. {
  55. Initialize(); //Ensure the client is initialized
  56. logger.Info($"** Connecting to SFTP host: {Host}");
  57. try
  58. {
  59. Client.Connect();
  60. Connected = true;
  61. }
  62. catch (SshOperationTimeoutException timeoutEx)
  63. {
  64. Connected = false;
  65. logger.Error("timeout: " + timeoutEx.ToString());
  66. Client.Disconnect();
  67. }
  68. catch (InvalidOperationException invOpEx)
  69. {
  70. if (invOpEx.Message.Contains("already connected"))
  71. {
  72. logger.Error("Exception indicates client already connected, let's disconnect");
  73. Client.Disconnect();
  74. Connected = false;
  75. }
  76. }
  77. catch (Exception ex)
  78. {
  79. Connected = false;
  80. logger.Error(ex.ToString());
  81. }
  82. }
  83. public void Disconnect()
  84. {
  85. if (Connected && Client != null)
  86. {
  87. logger.Info("** Disconnecting from SFTP host");
  88. Client.Disconnect();
  89. Connected = false;
  90. }
  91. }
  92. #endregion
  93. #region Upload methods
  94. //Consider implementing an asynchronous method here.
  95. public bool UploadSingleFile(string fileName, string hostFolder)
  96. {
  97. try
  98. {
  99. logger.Info($"** Current SFTP client connected? {Connected}");
  100. Client.ChangeDirectory(hostFolder);
  101. using (var uplfileStream = File.OpenRead(fileName))
  102. {
  103. Client.UploadFile(uplfileStream, Path.GetFileName(fileName), true);
  104. logger.Info($"** Uploaded File: {fileName}");
  105. }
  106. return true;
  107. }
  108. catch (Exception ex)
  109. {
  110. logger.Error("Upload exception: " + ex.ToString());
  111. return false;
  112. }
  113. }
  114. public bool ConnectAndSend(string fileName, string hostFolder)
  115. {
  116. using (var sftp = new SftpClient(Host, 22, UserName, Password))
  117. {
  118. logger.Info($"** Connecting {Host}, port: 22, username: {UserName}, password: {Password}");
  119. try
  120. {
  121. sftp.Connect();
  122. logger.Info("** Connected");
  123. sftp.ChangeDirectory(hostFolder);
  124. using (var uplfileStream = File.OpenRead(fileName))
  125. {
  126. sftp.UploadFile(uplfileStream, Path.GetFileName(fileName), true);
  127. logger.Info($"** Finished uploading - {fileName}");
  128. }
  129. sftp.Disconnect();
  130. logger.Info("** Disconnected");
  131. return true;
  132. }
  133. catch (Exception ex)
  134. {
  135. logger.Error(ex.ToString());
  136. return false;
  137. }
  138. }
  139. }
  140. #endregion
  141. }
  142. }