Guest User

Untitled

a guest
Jan 9th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. public int Upload(String HostName, String UserName, String Password, String remotePath, String localFilePath)
  2. {
  3. int result = 0;
  4. Session session = null;
  5. try
  6. {
  7. // Setup session options
  8. SessionOptions sessionOptions = new SessionOptions
  9. {
  10. Protocol = Protocol.Ftp,
  11. HostName = HostName,
  12. UserName = UserName,
  13. Password = Password,
  14. Timeout = TimeSpan.MaxValue,
  15. };
  16.  
  17. using (session = new Session())
  18. {
  19. // Connect
  20. session.Open(sessionOptions);
  21.  
  22. // upload files
  23. TransferOptions transferOptions = new TransferOptions();
  24. transferOptions.TransferMode = TransferMode.Ascii;
  25.  
  26. TransferOperationResult transferResult = null;
  27.  
  28. transferResult = session.PutFiles(localFilePath, remotePath, false, transferOptions);
  29.  
  30. // Throw on any error
  31. transferResult.Check();
  32. // Print results
  33. foreach (TransferEventArgs transfer in transferResult.Transfers)
  34. {
  35. Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
  36. }
  37.  
  38. }
  39.  
  40. result = 0;
  41. }
  42. catch (Exception e)
  43. {
  44. Console.WriteLine("Error: {0}", e);
  45. result = 1;
  46. }
  47. finally
  48. {
  49. if (session != null)
  50. {
  51. session.Dispose();
  52. }
  53. }
  54. return result;
  55. }
  56.  
  57. at System.TimeSpan.Add(TimeSpan ts)
  58. at System.TimeSpan.op_Addition(TimeSpan t1, TimeSpan t2)
  59. at WinSCP.Session.CheckForTimeout(String additional)
  60. at WinSCP.PatientFileStream.Wait(Int32& interval)
  61. at WinSCP.PatientFileStream.Read(Byte[] array, Int32 offset, Int32 count)
  62. at System.Xml.XmlTextReaderImpl.ReadData()
  63. at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
  64. at System.Xml.XmlTextReaderImpl.ParseText()
  65. at ProjectName.Upload(String HostName, String UName, String Password, String remotePath, String localFilePath)
Add Comment
Please, Sign In to add comment