Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. public class PSftp
  2. {
  3. public void PutFile(string localfile)
  4. {
  5. //Send Ftp Files - same idea as above - try...catch and try to repeat this code
  6. //if you can't connect the first time, timeout after a certain number of tries.
  7. var sessionOptions = new SessionOptions
  8. {
  9. Protocol = Protocol.Sftp,
  10. HostName = ConfigurationManager.AppSettings["sFTPhost"],
  11. UserName = ConfigurationManager.AppSettings["sFTPuid"],
  12. Password = ConfigurationManager.AppSettings["sFTPpwd"],
  13. PortNumber = int.Parse(ConfigurationManager.AppSettings["sFTPport"]),
  14. SshHostKeyFingerprint = ConfigurationManager.AppSettings["sFTPhostkey"]
  15.  
  16. };
  17.  
  18. using (var session = new Session())
  19. {
  20. session.SessionLogPath = ConfigurationManager.AppSettings["sFTPlogPath"];
  21. session.DisableVersionCheck = false;
  22. session.DefaultConfiguration = false;
  23. session.Open(sessionOptions); //Attempts to connect to your sFtp site
  24. //Get Ftp File
  25. var transferOptions = new TransferOptions
  26. {
  27. TransferMode = TransferMode.Binary,
  28. FilePermissions = null,
  29. PreserveTimestamp = false
  30. };
  31. //<em style="font-size: 9pt;">Automatic, Binary, or Ascii
  32. //null for default permissions. Can set user,
  33. //Group, or other Read/Write/Execute permissions.
  34. //destination file to that of source file - basically change the timestamp
  35. //to match destination and source files.
  36. transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
  37.  
  38.  
  39. //the parameter list is: local Path, Remote Path, Delete source file?, transfer Options
  40. TransferOperationResult transferResult = session.PutFiles(localfile, ConfigurationManager.AppSettings["sFTPInboxPath"], false, transferOptions);
  41. //Throw on any error
  42. transferResult.Check();
  43. //Log information and break out if necessary
  44. };
  45.  
  46. }
  47. }
  48.  
  49. //How to use the above class
  50. public void SaveFiletoFtp(string source)
  51. {
  52. var pftp = new PSftp();
  53. pftp.PutFile(source);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement