Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using WinSCP;
- class Example
- {
- public static int Main()
- {
- try
- {
- // Setup session options
- SessionOptions sessionOptions = new SessionOptions {
- Protocol = Protocol.Sftp,
- HostName = "example.com",
- UserName = "user",
- Password = "mypassword",
- SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
- };
- using (Session session = new Session())
- {
- // Connect
- session.Open(sessionOptions);
- RemoteDirectoryInfo directory = session.ListDirectory("/home/martin/public_html");
- foreach (RemoteFileInfo fileInfo in directory.Files)
- {
- Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}",
- fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime);
- }
- }
- return 0;
- }
- catch (Exception e)
- {
- Console.WriteLine("Error: {0}", e);
- return 1;
- }
- }
- }
- // Download the file and throw on any error
- session.GetFiles(remotePath, localPath).Check();
- public class HostSFTP
- {
- public List<string> downloadedFiles = new List<string>();
- public void FileSync(string hostName, string userName, string password, bool GiveUpSecurity, string SshHostKeyFingerprint, int port, string local, string remote, string exe, string fileMask)
- {
- SessionOptions sOpt = new SessionOptions
- {
- Protocol = Protocol.Sftp,
- HostName = hostName,
- UserName = userName,
- Password = password,
- GiveUpSecurityAndAcceptAnySshHostKey = GiveUpSecurity, -- You can choose to ignore sshhostupdates.
- SshHostKeyFingerprint = SshHostKeyFingerprint,
- PortNumber = port
- };
- try
- {
- using (Session session = new Session())
- {
- session.ExecutablePath = exe;// @"C:%Program folder%Winscpwinscp.exe";
- session.FileTransferred += FileTransferred;
- session.Open(sOpt);
- //Sync folders
- TransferOptions transferOpt = new TransferOptions();
- transferOpt.TransferMode = TransferMode.Binary;
- transferOpt.FileMask = fileMask;// @"filename.*|*" includes | excludes
- SynchronizationResult syncResult;
- syncResult =
- session.SynchronizeDirectories(
- SynchronizationMode.Local,
- local,
- remote,
- false,
- false,
- SynchronizationCriteria.None,
- transferOpt);
- syncResult.Check();
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("Error : {0}", e);
- }
- }
- public void FileTransferred(object sender, TransferEventArgs e)
- {
- if (e.Error == null)
- {
- Console.WriteLine("Download of {0} succeeded", e.FileName);
- downloadedFiles.Add(e.FileName.Replace(@"/", ""));
- }
- else
- {
- Console.WriteLine("Download of {0} failed: {1}", e.FileName, e.Error);
- }
- if (e.Chmod != null)
- {
- if (e.Chmod.Error == null)
- {
- Console.WriteLine("Permisions 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 with Session.SynchronizeDirectories
- Console.WriteLine("Timestamp of {0} kept with its default (current time)", e.Destination);
- }
- }
- }
Add Comment
Please, Sign In to add comment