将一些txt文件从本地文件夹上传到特定的FTP地址(我正在使用这个,ftp://ftpint/sales/to_system/)是我的日常例程之一。我正在使用ZappySys来自动化这个例程,但是我的公司不想再使用它了,所以我认为WinSCP是一个很好的选择。我已经安装了WinSCP 5.19 & .NET程序集,并遵循了这个链接ssis的说明。但我认为WinSCP不能识别我的FTP链接。这是我的C#代码,有什么建议吗?谢谢。
using System;
using WinSCP;
class Example
{
public static int Main()
{
try
{
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "xxx",
UserName = "xxx",
Password = "xxx",
SshHostKeyFingerprint = "SHA-256 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult =
session.PutFiles(@"C:\Users\Diomedas\test\*", "ftp://ftpint/sales/to_system/", false, transferOptions);
// Throw on any error
transferResult.Check();
// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
}
}
return 0;
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
return 1;
}
}
}发布于 2021-12-15 09:36:10
Session.PutFiles是一个远程路径。没有任何网址。所以应该是:
session.PutFiles(@"C:\Users\Diomedas\test\*", "/sales/to_system/", false, transferOptions);SessionOptions.HostName中指定了主机名。没有必要重复这些信息。Protocol.Sftp中指定了SessionOptions.Protocol,而您的URL有ftp://。确保您知道服务器的实际协议是什么。WinSCP GUI可以为您提供生成完整的工作代码(包括上传部分)。
https://stackoverflow.com/questions/70361171
复制相似问题