【问题标题】:WinSCP .NET assembly does not recognize FTP link/URL in Session.PutFilesWinSCP .NET 程序集无法识别 Session.PutFiles 中的 FTP 链接/URL
【发布时间】:2022-01-18 11:46:20
【问题描述】:

从本地文件夹上传一些 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:\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;
        }
    }
}

【问题讨论】:

    标签: c# ssis ftp winscp winscp-net


    【解决方案1】:

    remotePath argument of Session.PutFiles 是一个远程路径。不是任何网址。所以应该是这样的:

    session.PutFiles(@"C:\Users\Diomedas\test\*", "/sales/to_system/", false, transferOptions);
    
    • 您已经在SessionOptions.HostName 中指定了主机名。没有必要重复该信息。

    • 您的协议不匹配。您在SessionOptions.Protocol 中指定了Protocol.Sftp,而您的URL 具有ftp://。确保您知道服务器的实际协议是什么。


    WinSCP GUI 可以为你generate full working code (including the upload part)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2022-10-18
      • 2018-06-15
      相关资源
      最近更新 更多