Guest User

Untitled

a guest
Feb 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. using System;
  2. using WinSCP;
  3.  
  4. class Example
  5. {
  6. public static int Main()
  7. {
  8. try
  9. {
  10. // Setup session options
  11. SessionOptions sessionOptions = new SessionOptions {
  12. Protocol = Protocol.Sftp,
  13. HostName = "example.com",
  14. UserName = "user",
  15. Password = "mypassword",
  16. SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
  17. };
  18.  
  19. using (Session session = new Session())
  20. {
  21. // Connect
  22. session.Open(sessionOptions);
  23.  
  24. RemoteDirectoryInfo directory = session.ListDirectory("/home/martin/public_html");
  25.  
  26. foreach (RemoteFileInfo fileInfo in directory.Files)
  27. {
  28. Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}",
  29. fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime);
  30. }
  31. }
  32.  
  33. return 0;
  34. }
  35. catch (Exception e)
  36. {
  37. Console.WriteLine("Error: {0}", e);
  38. return 1;
  39. }
  40. }
  41. }
  42.  
  43. // Download the file and throw on any error
  44. session.GetFiles(remotePath, localPath).Check();
  45.  
  46. public class HostSFTP
  47. {
  48. public List<string> downloadedFiles = new List<string>();
  49.  
  50. public void FileSync(string hostName, string userName, string password, bool GiveUpSecurity, string SshHostKeyFingerprint, int port, string local, string remote, string exe, string fileMask)
  51. {
  52. SessionOptions sOpt = new SessionOptions
  53. {
  54. Protocol = Protocol.Sftp,
  55. HostName = hostName,
  56. UserName = userName,
  57. Password = password,
  58. GiveUpSecurityAndAcceptAnySshHostKey = GiveUpSecurity, -- You can choose to ignore sshhostupdates.
  59. SshHostKeyFingerprint = SshHostKeyFingerprint,
  60. PortNumber = port
  61. };
  62.  
  63. try
  64. {
  65. using (Session session = new Session())
  66. {
  67. session.ExecutablePath = exe;// @"C:%Program folder%Winscpwinscp.exe";
  68. session.FileTransferred += FileTransferred;
  69. session.Open(sOpt);
  70.  
  71. //Sync folders
  72. TransferOptions transferOpt = new TransferOptions();
  73. transferOpt.TransferMode = TransferMode.Binary;
  74. transferOpt.FileMask = fileMask;// @"filename.*|*" includes | excludes
  75.  
  76. SynchronizationResult syncResult;
  77. syncResult =
  78. session.SynchronizeDirectories(
  79. SynchronizationMode.Local,
  80. local,
  81. remote,
  82. false,
  83. false,
  84. SynchronizationCriteria.None,
  85. transferOpt);
  86.  
  87. syncResult.Check();
  88. }
  89. }
  90.  
  91. catch (Exception e)
  92. {
  93. Console.WriteLine("Error : {0}", e);
  94. }
  95. }
  96.  
  97. public void FileTransferred(object sender, TransferEventArgs e)
  98. {
  99. if (e.Error == null)
  100. {
  101. Console.WriteLine("Download of {0} succeeded", e.FileName);
  102. downloadedFiles.Add(e.FileName.Replace(@"/", ""));
  103. }
  104. else
  105. {
  106. Console.WriteLine("Download of {0} failed: {1}", e.FileName, e.Error);
  107. }
  108. if (e.Chmod != null)
  109. {
  110. if (e.Chmod.Error == null)
  111. {
  112. Console.WriteLine("Permisions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions);
  113. }
  114. else
  115. {
  116. Console.WriteLine("Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error);
  117. }
  118. }
  119. else
  120. {
  121. Console.WriteLine("Permissions of {0} kept with their defaults", e.Destination);
  122. }
  123.  
  124. if (e.Touch != null)
  125. {
  126. if (e.Touch.Error == null)
  127. {
  128. Console.WriteLine("Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime);
  129. }
  130. else
  131. {
  132. Console.WriteLine("Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error);
  133. }
  134. }
  135. else
  136. {
  137. // This should never happen with Session.SynchronizeDirectories
  138. Console.WriteLine("Timestamp of {0} kept with its default (current time)", e.Destination);
  139. }
  140. }
  141. }
Add Comment
Please, Sign In to add comment