Created
May 31, 2018 19:19
-
-
Save luizhenriquesoares/a8f66cb664cf4c950b3501cf5ea365ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using FluentFTP; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Threading.Tasks; | |
using WinSCP; | |
namespace MoverArquivoFTP | |
{ | |
public class FtpGui | |
{ | |
public static bool MoveFile(string Fromdirectory, string pattern, string Todirectory) | |
{ | |
var DirectoryDownload = @"C:\RPA\FTP\DownloadFTP"; | |
var FileName = Directory.GetFiles(DirectoryDownload, pattern).First(); | |
return true; | |
} | |
public static bool DownloadGeneral(string directory, FTPContract contract) | |
{ | |
SessionOptions sessionOptions = new SessionOptions | |
{ | |
Protocol = Protocol.Ftp, | |
HostName = contract.ip, | |
PortNumber = 21, | |
UserName = contract.username, | |
Password = contract.password, | |
FtpSecure = FtpSecure.Explicit, | |
GiveUpSecurityAndAcceptAnyTlsHostCertificate = true | |
}; | |
using (Session session = new Session()) | |
{ | |
// Connect | |
session.Open(sessionOptions); | |
// Download files | |
TransferOptions transferOptions = new TransferOptions(); | |
transferOptions.TransferMode = TransferMode.Binary; | |
TransferOperationResult transferResult; | |
transferResult = | |
session.GetFiles(contract.path, directory, false, transferOptions); | |
// Throw on any error | |
transferResult.Check(); | |
// Print results | |
foreach (TransferEventArgs transfer in transferResult.Transfers) | |
{ | |
Console.WriteLine("Download of {0} succeeded", transfer.FileName); | |
} | |
} | |
return true; | |
} | |
public static bool SendFileWinSCP(string directory, FTPContract contract) | |
{ | |
SessionOptions sessionOptions = new SessionOptions | |
{ | |
Protocol = Protocol.Ftp, | |
HostName = contract.ip, | |
PortNumber = 21, | |
UserName = contract.username, | |
Password = contract.password, | |
FtpSecure = FtpSecure.Explicit, | |
GiveUpSecurityAndAcceptAnyTlsHostCertificate = true | |
}; | |
try | |
{ | |
using (Session session = new Session()) | |
{ | |
session.FileTransferred += FileTransferred; | |
session.Open(sessionOptions); | |
SynchronizationResult synchronizationResult; | |
synchronizationResult = | |
session.SynchronizeDirectories( | |
SynchronizationMode.Remote, directory, | |
contract.path, false); | |
synchronizationResult.Check(); | |
} | |
return true; | |
} | |
catch(Exception e) | |
{ | |
Console.Write(e); | |
} | |
return false; | |
} | |
private static void FileTransferred(object sender, TransferEventArgs e) | |
{ | |
if (e.Error == null) | |
{ | |
Console.WriteLine("Upload of {0} succeeded", e.FileName); | |
} | |
else | |
{ | |
Console.WriteLine("Upload of {0} failed: {1}", e.FileName, e.Error); | |
} | |
if (e.Chmod != null) | |
{ | |
if (e.Chmod.Error == null) | |
{ | |
Console.WriteLine( | |
"Permissions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions); | |
} | |
else | |
{ | |
Console.WriteLine( | |
"Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error); | |
} | |
} | |
else | |
{ | |
Console.WriteLine("Permissions of {0} kept with their defaults", e.Destination); | |
} | |
if (e.Touch != null) | |
{ | |
if (e.Touch.Error == null) | |
{ | |
Console.WriteLine( | |
"Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime); | |
} | |
else | |
{ | |
Console.WriteLine( | |
"Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error); | |
} | |
} | |
else | |
{ | |
// This should never happen during "local to remote" synchronization | |
Console.WriteLine( | |
"Timestamp of {0} kept with its default (current time)", e.Destination); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment