static void Main(string[] args) { try { WinSCP.Session session = new WinSCP.Session(); session.Open(Program.sessionOptions.getSessionOptions()); if (session.Opened) { Console.WriteLine("Connected successfully"); Backup.BackupOptions backupOptions = new Backup.BackupOptions(); foreach (string remoteDirectory in backupOptions.getRemoteDirectories()) { session.SynchronizeDirectories(WinSCP.SynchronizationMode.Local, backupOptions.getBackupDirectory(), remoteDirectory, false); Console.WriteLine("backed up: " + remoteDirectory); } //session.GetFilesToDirectory(downloadBackupDirectory, @"C:\Users\adsy2\WS Backups\automated"); } session.Close(); Console.WriteLine("Disconnected"); } catch (Exception exception) { Console.WriteLine("Failed connection: " + exception.Message); Console.Read(); } }
private bool UploadToFtp(string filename) { try { var sessionOptions = new WinSCP.SessionOptions { FtpSecure = WinSCP.FtpSecure.None, Protocol = WinSCP.Protocol.Ftp, HostName = ConfigurationManager.AppSettings["FTPHost"], PortNumber = int.Parse(ConfigurationManager.AppSettings["FTPPort"]), UserName = ConfigurationManager.AppSettings["FTPUsername"], Password = ConfigurationManager.AppSettings["FTPPassword"], }; string ftpRoot = ConfigurationManager.AppSettings["FTPRoot"]; using (var session = new WinSCP.Session()) { session.Open(sessionOptions); Log("Connected successfully to FTP server"); var transferOptions = new WinSCP.TransferOptions() { TransferMode = WinSCP.TransferMode.Binary }; //Ceheck and create folder String mftpFilePath = string.Format("/{0}/{1}年/{2}月/{3}/{4}.pdf", ftpRoot, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.ToString("yyyyMMddHHmmss")); //session.CreateDirectory(Constants.FTPRoot); FileInfo fi = new FileInfo(filename); var hResult = session.PutFiles(fi.FullName, mftpFilePath, false, transferOptions); hResult.Check(); } return(true); } catch { return(false); } }