//private string hostname, username, password; public static void UploadFiles(ConfigurationProfile profile, string temppath, string remotedir) { // Setup session options // add support for: scp/sftp protocols, ssh host/private keys, active/passive mode, port, FtpSecure, timeout, ssl cert, using (FTPSClient session = new FTPSClient()) { ESSLSupportMode sslSupportMode = ESSLSupportMode.ClearText; RemoteCertificateValidationCallback userValidateServerCertificate; userValidateServerCertificate = new RemoteCertificateValidationCallback(ValidateServerCertificate); // enable encryption if desired if (profile.Encryption.IsTrue()) { sslSupportMode |= ESSLSupportMode.ControlAndDataChannelsRequired | ESSLSupportMode.CredentialsRequired; if (profile.EncryptionImplicit.IsTrue()) { // implicit if desired sslSupportMode |= ESSLSupportMode.Implicit; } if (profile.ForceEncryption.IsTrue()) { // force encryption if desired userValidateServerCertificate = new RemoteCertificateValidationCallback(delegate { return true; }); } } session.Connect(profile.Hostname, new System.Net.NetworkCredential(profile.Username, profile.Password), sslSupportMode, userValidateServerCertificate); // Upload files //TransferOptions transferOptions = new TransferOptions(); //transferOptions.TransferMode = TransferMode.Binary; //TransferOperationResult transferResult; //transferResult = session.PutFiles(Path.Combine(temppath, "*"), Common.Parse(remotedir), false, transferOptions); try { session.SetCurrentDirectory(Common.ParseTemplate(remotedir)); } catch { session.MakeDir(Common.ParseTemplate(remotedir)); } session.PutFiles(temppath, Common.ParseTemplate(remotedir), "*", EPatternStyle.Wildcard, false, new FileTransferCallback(TransferCallback)); // Throw on any error //transferResult.Check(); // Print results //foreach (TransferEventArgs transfer in transferResult.Transfers) //{ // Console.WriteLine("Upload of {0} succeeded", transfer.FileName); //} } }
public Repository(string host, Token token, string repoFolderPath) { _ftp = new FTPSClient(); _ftp.Connect(host, new NetworkCredential(token.Key, token.Secret), ESSLSupportMode.ClearText); try { _ftp.MakeDir(repoFolderPath); } catch {} _ftp.SetCurrentDirectory(repoFolderPath); }
public void GetFilesWithFTPNewMode(string host, string remoteLocation) { FTPSClient client = new FTPSClient(); try { client.Connect(host,ESSLSupportMode.All); try { client.SetCurrentDirectory(remoteLocation); ScriptName = client.GetCurrentDirectory(); string localDownloadDirectory = downloadDir + ScriptName; client.GetFiles(localDownloadDirectory, "*.log", EPatternStyle.Wildcard, false); } catch (Exception ex) { } } finally { client.Close(); client.Dispose(); } }
/// <summary> /// FTPS procedure to upload or download files /// </summary> /// <param name="blShuttingDown"></param> public void FTPs(ref bool blShuttingDown) { FTPSClient FTPS = null; bool blFileFound = false; bool blOverwriteFile = false; try { FTPS = new FTPSClient(); AES256 aes = new AES256(ep); string upassword = aes.Decrypt(_password); if (Protocol == ProtocolOptions.FTP) { FTPS.Connect(Host, Port, new NetworkCredential(Username, upassword), ESSLSupportMode.ClearText, null, null, 0, 0, 0, Timeout); } else { if (AllowAnyCertificate) { if (Protocol == ProtocolOptions.FTPsExplicit) { FTPS.Connect(Host, Port, new NetworkCredential(Username, upassword), ESSLSupportMode.CredentialsRequired | ESSLSupportMode.DataChannelRequested, new RemoteCertificateValidationCallback(ValidateTestServerCertificate), null, 0, 0, 0, Timeout); } else { FTPS.Connect(Host, Port, new NetworkCredential(Username, upassword), ESSLSupportMode.Implicit, new RemoteCertificateValidationCallback(ValidateTestServerCertificate), null, 0, 0, 0, Timeout); } } else { if (Protocol == ProtocolOptions.FTPsExplicit) { FTPS.Connect(Host, Port, new NetworkCredential(Username, upassword), ESSLSupportMode.CredentialsRequired | ESSLSupportMode.DataChannelRequested, new RemoteCertificateValidationCallback(ValidateServerCertificate), null, 0, 0, 0, Timeout); } else { FTPS.Connect(Host, Port, new NetworkCredential(Username, upassword), ESSLSupportMode.Implicit, new RemoteCertificateValidationCallback(ValidateServerCertificate), null, 0, 0, 0, Timeout); } } } _evt.WriteEntry("Remote Sync: FTPS Connected Successfully to: " + Host, System.Diagnostics.EventLogEntryType.Information, 2005, 20); upassword = ""; switch (TransferDirection) { case TransferDirectionOptions.Upload: BackupFolder = BackupFolder.Replace("\\\\", "\\"); IList<DirectoryListItem> RemoteFilesU=null; try { FTPS.SetCurrentDirectory(RemoteDirectory); RemoteFilesU = FTPS.GetDirectoryList(); UploadFiles = Common.WalkDirectory(BackupFolder, ref blShuttingDown, FileNameFilter); //CreateRemote Directories foreach (DirectoryInfo dir in Common.GetAllDirectories(BackupFolder)) { string strRemotePath = ""; strRemotePath=Common.RemotePathCombine(RemoteDirectory, dir.FullName, BackupFolder); if (blShuttingDown) { throw new Exception("Shutting Down"); } if (blShuttingDown) { _evt.WriteEntry("Remote Sync: Shutting down about to possibly create a folder on Host: " + Host + " Folder: " + strRemotePath, System.Diagnostics.EventLogEntryType.Information, 2040, 20); return; } try { FTPS.MakeDir(strRemotePath); _evt.WriteEntry("Remote Sync: Folder Created on " + Host + " : " + strRemotePath, System.Diagnostics.EventLogEntryType.Information, 2040, 20); } catch (Exception) { } //FTPS.SetCurrentDirectory(strRemotePath); } //Upload Each File foreach (FileInfo fileU in UploadFiles) { if (blShuttingDown) { throw new Exception("Shutting Down"); } try { string strRemotePath = ""; strRemotePath = Common.RemotePathCombine(RemoteDirectory, fileU.DirectoryName, BackupFolder); strRemotePath = Common.RemotePathCombine(strRemotePath, fileU.Name); blFileFound = false; blOverwriteFile = false; try { if (FTPS.GetFileTransferSize(strRemotePath) > 0) { blFileFound = true; if (!(/*(fileU.LastAccessTimeUtc == FTPS.GetFileModificationTime(strRemotePath)) &&*/ ((ulong)fileU.Length == FTPS.GetFileTransferSize(strRemotePath)))) { blOverwriteFile = true; } } } catch (Exception) { //File Not found No Action Necessary on Error } try { if (FTPS.GetFileTransferSize(strRemotePath + ".7z") > 0) { blFileFound = true; //7z file exists but there is no current way to compare the 7zipped file vs the non compressed file so the user overwrite option will force the appropriate action blOverwriteFile = true; /* if (!((fileU.LastAccessTimeUtc == FTPS.GetFileModificationTime(strRemotePath + ".7z")) && ((ulong)fileU.Length == FTPS.GetFileTransferSize(strRemotePath + ".7z")))) { blOverwriteFile = true; } */ } } catch (Exception) { //File Not Found No Action Necessary on Error } if (blShuttingDown) { _evt.WriteEntry("Remote Sync FTPS: Shutting Down about to possible upload a file: " + fileU.FullName + " Host: " + Host + " To: " + strRemotePath, System.Diagnostics.EventLogEntryType.Information, 2010, 20); return; } if ((!blFileFound || blOverwriteFile || (Overwrite == OverwriteOptions.ForceOverwrite && blFileFound)) && !(Overwrite == OverwriteOptions.NoOverwrite && blFileFound)) { //This Uploads the file FTPS.PutFile(fileU.FullName, strRemotePath); _evt.WriteEntry("Remote Sync FTPS: File Uploaded: " + fileU.FullName + " Host: " + Host + " To: " + strRemotePath, System.Diagnostics.EventLogEntryType.Information, 2010, 20); } } catch (Exception exp) { _evt.WriteEntry("Remote Sync FTPS: Host:" + Host + " Upload FileName: " + fileU.FullName + " Error: " + exp.Message, System.Diagnostics.EventLogEntryType.Error, 2010, 20); } } } catch (Exception exu) { _evt.WriteEntry("Remote Sync FTPS: Upload Error on Host:" + Host + " Error: " + exu.Message, System.Diagnostics.EventLogEntryType.Error, 2010, 20); } finally { if (RemoteFilesU != null) { RemoteFilesU.Clear(); } if (UploadFiles != null) { UploadFiles.Clear(); } RemoteFilesU = null; UploadFiles = null; } break; case TransferDirectionOptions.Download: List<RemoteFile> RemoteFilesD=null; try { FTPS.SetCurrentDirectory(RemoteDirectory); RemoteFilesD = Common.GetRemoteDirectories(RemoteDirectory, FTPS, ""); Common.CreateLocalFolderStructure(RemoteFilesD, BackupFolder); foreach (RemoteFile FileD in RemoteFilesD) { string strLocalFile = ""; string strRemoteFile = ""; if (blShuttingDown) { throw new Exception("Shutting Down"); } try { if (!FileD.IsDirectory) { strLocalFile = Common.WindowsPathCombine(BackupFolder, FileD.ParentDirectory); strLocalFile = Common.WindowsPathCombine(strLocalFile, FileD.Name); strRemoteFile = FileD.FullName; if (Common.DownloadFile(strLocalFile, strRemoteFile, FileD, Overwrite)) { if ((!blFileFound || blOverwriteFile || (Overwrite == OverwriteOptions.ForceOverwrite && blFileFound)) && !(Overwrite == OverwriteOptions.NoOverwrite && blFileFound)) { if (Common.FixNullstring(FileNameFilter) != "" && Common.VerifyPattern(FileNameFilter)) { if (Common.FileNameMatchesPattern(FileNameFilter, FileD.Name)) { FTPS.GetFile(strRemoteFile, strLocalFile); _evt.WriteEntry("Remote Sync FTPS: File Downloaded: " + strRemoteFile + " Host: " + Host + " To: " + strLocalFile, System.Diagnostics.EventLogEntryType.Information, 2020, 20); } } else { FTPS.GetFile(strRemoteFile, strLocalFile); _evt.WriteEntry("Remote Sync FTPS: File Downloaded: " + strRemoteFile + " Host: " + Host + " To: " + strLocalFile, System.Diagnostics.EventLogEntryType.Information, 2020, 20); } } } } } catch (Exception exdi) { _evt.WriteEntry("Remote Sync FTPS: File Download Error: " + strRemoteFile + " Host: " + Host + " To: " + strLocalFile + " Error: " + exdi.Message, System.Diagnostics.EventLogEntryType.Error, 2020, 20); } } } catch (Exception exd) { _evt.WriteEntry("Remote Sync FTPS: Download Error: Host: " + Host + " Error: " + exd.Message, System.Diagnostics.EventLogEntryType.Error, 2020, 20); } finally { if (RemoteFilesD !=null) { RemoteFilesD.Clear(); } RemoteFilesD = null; } break; } } catch (Exception ex) { _evt.WriteEntry("Remote Sync FTPS: Error: Host: " + Host + " Error: " + ex.Message, System.Diagnostics.EventLogEntryType.Error, 2000, 20); } finally { if (FTPS != null) { try { FTPS.Close(); } catch (Exception) { } FTPS.Dispose(); } FTPS = null; } }