将一些txt文件从本地文件夹上传到特定的FTP地址(我使用的是ftp://ftpint/sales/to_system/
(是我的日常工作之一。我正在使用ZappySys来自动化这个例程,但我的公司不想再使用它了,所以我认为WinSCP可能是一个不错的选择。我已经安装了WinSCP 5.19&NET程序集,并遵循此链接中的说明,https://winscp.net/eng/docs/library_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:UsersDiomedastest*", "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;
}
}
}
Session.PutFiles
的remotePath
参数是远程路径。没有任何URL。所以应该是这样的:
session.PutFiles(@"C:UsersDiomedastest*", "/sales/to_system/", false, transferOptions);
您已经在
SessionOptions.HostName
中指定了主机名。重复这些信息毫无意义。您的协议不匹配。您已在
SessionOptions.Protocol
中指定了Protocol.Sftp
,而您的URL具有ftp://
。确保您知道服务器的实际协议是什么。
WinSCP GUI可以为您生成完整的工作代码(包括上传部分(。