/// <summary> /// Synchronizes directories. /// </summary> /// <param name="url">Session URL (https://winscp.net/eng/docs/session_url).</param> /// <param name="remoteFolder">Full path to remote directory.</param> /// <param name="localFolder">Full path to local directory.</param> /// <param name="removeFiles">When set to true, deletes obsolete files.</param> public void SynchronizeDirectories( string url, string remoteFolder, string localFolder, bool removeFiles = false) { if (string.IsNullOrWhiteSpace(url)) { throw new ArgumentException(nameof(url)); } if (string.IsNullOrWhiteSpace(remoteFolder)) { throw new ArgumentException(nameof(remoteFolder)); } if (string.IsNullOrWhiteSpace(localFolder)) { throw new ArgumentException(nameof(localFolder)); } if (!Directory.Exists(localFolder)) { throw new DirectoryNotFoundException($"{localFolder} is not found"); } var sessionOptions = new SessionOptions(); sessionOptions.ParseUrl(url); Logger.Information($"Synchronize directories on {url}"); SynchronizeDirectories(sessionOptions, remoteFolder, localFolder, removeFiles); }
public override void Configure(IConfigSectionNode node) { m_Options = new SessionOptions(); var serverUrlAttr = node.AttrByName(CONFIG_SERVER_URL_ATTR); if (serverUrlAttr.Exists) { var serverUrl = serverUrlAttr.ValueAsString(); if (serverUrl.IsNotNullOrWhiteSpace()) { m_Options.ParseUrl(serverUrl); } } var protocolAttr = node.AttrByName(CONFIG_PROTOCOL_ATTR); if (protocolAttr.Exists) { m_Options.Protocol = protocolAttr.ValueAsEnum(Protocol.Sftp); } base.Configure(node); var rawSettings = node[CONFIG_RAW_SETTINGS_SECTION]; if (rawSettings.Exists) { foreach (var attr in rawSettings.Attributes) { m_Options.AddRawSettings(attr.Name, attr.ValueAsString()); } } }
/// <summary> /// Synchronizes directories. /// </summary> /// <param name="url">Session URL (https://winscp.net/eng/docs/session_url).</param> /// <param name="remoteFolder">Full path to remote directory.</param> /// <param name="localFolder">Full path to local directory.</param> /// <param name="removeFiles">When set to true, deletes obsolete files.</param> public void SynchronizeDirectories( string url, string remoteFolder, string localFolder, bool removeFiles = false) { if (string.IsNullOrWhiteSpace(url)) { throw new ArgumentException(nameof(url)); } if (string.IsNullOrWhiteSpace(remoteFolder)) { throw new ArgumentException(nameof(remoteFolder)); } if (string.IsNullOrWhiteSpace(localFolder)) { throw new ArgumentException(nameof(localFolder)); } if (!Directory.Exists(localFolder)) { throw new DirectoryNotFoundException($"{localFolder} is not found"); } var sessionOptions = new SessionOptions(); sessionOptions.ParseUrl(url); sessionOptions.AddRawSettings("LocalDirectory", _context.Environment.WorkingDirectory.FullPath); using (var session = new Session()) { Logger.Log($"Synchronize directories on {url}"); session.FileTransferred += OnFileTransferred; session.Open(sessionOptions); var result = session.SynchronizeDirectories(SynchronizationMode.Remote, localFolder, remoteFolder, removeFiles); result.Check(); Logger.Log($"{localFolder} and {remoteFolder} were synchronized"); } }
private Task <int> getPendingFilesCountAsync() { return(Task <int> .Factory.StartNew(() => { try { var sessionOptions = new SessionOptions(); sessionOptions.ParseUrl("sftp://" + sftpParms.SftpUrl); sessionOptions.FtpMode = FtpMode.Passive; sessionOptions.Protocol = Protocol.Sftp; sessionOptions.UserName = sftpParms.SftpUserName; sessionOptions.Password = sftpParms.SftpPassword; sessionOptions.SshHostKeyFingerprint = sftpParms.SftpHostFingerprint; var winscpExePath = $@"{sftpParms.WinScpBinDirectory}\winscp.exe"; if (!File.Exists(winscpExePath)) { throw new Exception($"WinScp executable is not found at path '{winscpExePath}'."); } using (Session session = new Session()) { Log.LogInformation("Before opening WinScp session..."); session.ExecutablePath = winscpExePath; session.Open(sessionOptions); Log.LogInformation("WinScp session successfully opened."); var dirList = session.ListDirectory(@"/upload/get"); int fileCount = dirList.Files.Where(f => f.Name.Contains(".txt")).Count(); session.Close(); Log.LogInformation("WinScp session closed."); return fileCount; } } catch (Exception ex) { Log.LogInformation("Exception occurred in getPendingFilesCountAsync." + Environment.NewLine + ex.ToReport()); throw new Exception("An exception occurred while attempting to get the count of pending files.", ex); } })); }