123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using NLog;
- using Renci.SshNet;
- using Renci.SshNet.Common;
- namespace Dfs.WayneChina.FairbanksRTData
- {
- /// <summary>
- /// The SFTP client is implemented using SSH.NET 2016.1.0
- /// </summary>
- public class FairbanksSftpClient
- {
- #region Properties
- public string Host { get; private set; }
- public int Port { get; private set; }
- public string UserName { get; set; }
- public string Password { get; set; }
- public SftpClient Client { get; private set; }
- public bool Connected { get; set; } = false;
- #endregion
- #region Logger
- NLog.Logger logger = NLog.LogManager.LoadConfiguration("NLog.config").GetLogger("Fairbanks");
- #endregion
- #region Constructor
- public FairbanksSftpClient(string host, string username, string password)
- {
- RefreshConfig(host, username, password);
- }
- #endregion
- #region Refresh Config
- public void RefreshConfig(string host, string username, string password)
- {
- Host = host;
- UserName = username;
- Password = password;
- //Reset the connection
- Client = null;
- Connected = false;
- }
- #endregion
- #region Initialize
- public void Initialize()
- {
- if (Client != null)
- return;
- logger.Info($"Initializing SFTP client against: {Host}, {UserName}, {Password}");
- Client = new SftpClient(Host, 22, UserName, Password);
- }
- #endregion
- #region Connection (connect and disconnect)
- public void Connect()
- {
- Initialize(); //Ensure the client is initialized
- logger.Info($"** Connecting to SFTP host: {Host}");
- try
- {
- Client.Connect();
- Connected = true;
- }
- catch (SshOperationTimeoutException timeoutEx)
- {
- Connected = false;
- logger.Error("timeout: " + timeoutEx.ToString());
- Client.Disconnect();
- }
- catch (InvalidOperationException invOpEx)
- {
- if (invOpEx.Message.Contains("already connected"))
- {
- logger.Error("Exception indicates client already connected, let's disconnect");
- Client.Disconnect();
- Connected = false;
- }
- }
- catch (Exception ex)
- {
- Connected = false;
- logger.Error(ex.ToString());
- }
- }
- public void Disconnect()
- {
- if (Connected && Client != null)
- {
- logger.Info("** Disconnecting from SFTP host");
- Client.Disconnect();
- Connected = false;
- }
- }
- #endregion
- #region Upload methods
- //Consider implementing an asynchronous method here.
- public bool UploadSingleFile(string fileName, string hostFolder)
- {
- try
- {
- logger.Info($"** Current SFTP client connected? {Connected}");
- Client.ChangeDirectory(hostFolder);
- using (var uplfileStream = File.OpenRead(fileName))
- {
- Client.UploadFile(uplfileStream, Path.GetFileName(fileName), true);
- logger.Info($"** Uploaded File: {fileName}");
- }
- return true;
- }
- catch (Exception ex)
- {
- logger.Error("Upload exception: " + ex.ToString());
- return false;
- }
- }
- public bool ConnectAndSend(string fileName, string hostFolder)
- {
- using (var sftp = new SftpClient(Host, 22, UserName, Password))
- {
- logger.Info($"** Connecting {Host}, port: 22, username: {UserName}, password: {Password}");
- try
- {
- sftp.Connect();
- logger.Info("** Connected");
- sftp.ChangeDirectory(hostFolder);
- using (var uplfileStream = File.OpenRead(fileName))
- {
- sftp.UploadFile(uplfileStream, Path.GetFileName(fileName), true);
- logger.Info($"** Finished uploading - {fileName}");
- }
- sftp.Disconnect();
- logger.Info("** Disconnected");
- return true;
- }
- catch (Exception ex)
- {
- logger.Error(ex.ToString());
- return false;
- }
- }
- }
- #endregion
- }
- }
|