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 { /// /// The SFTP client is implemented using SSH.NET 2016.1.0 /// 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 } }