SftpWinSCPUtils.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.ServiceModel;
  5. using System;
  6. using System.Net;
  7. using System.Collections;
  8. using WinSCP;
  9. using Kede;
  10. using System.Text;
  11. using System.IO;
  12. namespace TestAgreement.utils
  13. {
  14. internal class SftpWinSCPUtils
  15. {
  16. // 上传
  17. public static ResponseBody sftpUpload(string localFilePath,string remoteFilePath,string ServerUrl, string UserName, string Password,string PortNumber) {
  18. // 本地文件
  19. //localFilePath = @"D:\X.nc";
  20. ResponseBody responseBody = new ResponseBody();
  21. if (!File.Exists(localFilePath))
  22. {
  23. responseBody.code = 0;
  24. responseBody.msg = $"File '{localFilePath}' does not exist.";
  25. return responseBody;
  26. }
  27. //localFilePath =localFilePath.Replace(@"\\",@"\");
  28. // 远程文件地址
  29. //remoteFilePath = "/testNc/X.nc";
  30. //remoteFilePath = remoteFilePath;
  31. responseBody.code = 1;
  32. // 设定 SFTP 连接选项
  33. SessionOptions sessionOptions = new SessionOptions
  34. {
  35. Protocol = Protocol.Sftp,
  36. HostName = ServerUrl, // SFTP 服务器地址
  37. UserName = UserName, // 用户名
  38. Password = Password, // 密码
  39. PortNumber = PortNumber.StringToInt(),
  40. };
  41. // 使用 WinSCP 会话
  42. using (Session session = new Session())
  43. {
  44. // 连接到 SFTP 服务器
  45. try
  46. {
  47. // 设置不用公私钥的一定要设置这句话
  48. sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = true;
  49. // 不用公私钥的要这一句
  50. sessionOptions.FtpSecure = FtpSecure.None;
  51. Console.WriteLine("scp =======================");
  52. session.Open(sessionOptions);
  53. Console.WriteLine("scp end =======================");
  54. // 执行上传
  55. TransferOperationResult transferResult;
  56. transferResult = session.PutFiles(localFilePath, remoteFilePath, false);
  57. // 检查传输结果
  58. transferResult.Check();
  59. // 输出上传的文件名
  60. foreach (TransferEventArgs transfer in transferResult.Transfers)
  61. {
  62. Console.WriteLine("Uploaded: " + transfer.FileName);
  63. }
  64. }
  65. catch (Exception e)
  66. {
  67. Console.WriteLine("Error: " + e.Message);
  68. responseBody.code = 0;
  69. responseBody.msg = e.Message;
  70. }
  71. finally
  72. {
  73. // 断开连接
  74. session.Dispose();
  75. }
  76. return responseBody;
  77. }
  78. }
  79. /*
  80. 下载nc程序
  81. */
  82. public void SftpDownload()
  83. {
  84. Console.WriteLine("====================sftp文件下载=========================");
  85. // 远程文件路径
  86. string localFilePath = @"C:\Users\Lenovo\Desktop\temp\testNcQ.nc";
  87. // 本地文件路径
  88. string remoteFilePath = "/testNc/testNcQ.nc";
  89. // 设定 SFTP 连接选项
  90. SessionOptions sessionOptions = new SessionOptions
  91. {
  92. Protocol = Protocol.Sftp,
  93. HostName = "192.168.11.39", // SFTP 服务器地址
  94. UserName = "root", // 用户名l
  95. Password = "root", // 密码
  96. PortNumber = 8081,
  97. };
  98. // 使用 WinSCP 会话
  99. using (Session session = new Session())
  100. {
  101. try
  102. {
  103. // 连接到 SFTP 服务器
  104. session.Open(sessionOptions);
  105. Console.WriteLine("Connected to SFTP server.");
  106. // 执行下载
  107. TransferOperationResult transferResult = session.GetFiles(remoteFilePath, localFilePath, false);
  108. transferResult.Check(); // 检查传输结果
  109. // 输出下载的文件名
  110. foreach (TransferEventArgs transfer in transferResult.Transfers)
  111. {
  112. Console.WriteLine("Downloaded: " + transfer.FileName);
  113. }
  114. }
  115. catch (Exception e)
  116. {
  117. Console.WriteLine("Error: " + e.Message);
  118. }
  119. finally
  120. {
  121. // 断开连接
  122. session.Dispose();
  123. }
  124. }
  125. }
  126. }
  127. }