【问题标题】:WinSCP - Uploading files from Windows machine to Linux - Error: Timespan overflowed because the duration is too longWinSCP - 将文件从 Windows 机器上传到 Linux - 错误:时间跨度溢出,因为持续时间太长
【发布时间】:2019-01-09 13:30:11
【问题描述】:

我从托管 NuGet 包安装了 WinSCP。

我正在尝试使用 WinSCP 将文件从 Windows 上传到 Linux 服务器。我收到错误为

时间跨度溢出,因为持续时间太长。

我已尝试使用以下代码上传文件。

public int Upload(
    String HostName, String UserName, String Password, String remotePath,
    String localFilePath)
{
    int result = 0;
    Session session = null;
    try
    {
        // Setup session options               
        SessionOptions sessionOptions = new SessionOptions
        {
            Protocol = Protocol.Ftp,
            HostName = HostName,
            UserName = UserName,
            Password = Password,
            Timeout = TimeSpan.MaxValue,
        };

        using (session = new Session())
        {
            // Connect
            session.Open(sessionOptions);

           // upload files
            TransferOptions transferOptions = new TransferOptions();
            transferOptions.TransferMode = TransferMode.Ascii;

            TransferOperationResult transferResult = null;

            transferResult =
                session.PutFiles(localFilePath, remotePath, false, transferOptions);

            // Throw on any error
            transferResult.Check();
            // Print results
            foreach (TransferEventArgs transfer in transferResult.Transfers)
            {
                Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
            }

        }

        result = 0;
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: {0}", e);
        result = 1;
    }
    finally
    {
        if (session != null)
        {
            session.Dispose();
        }
    }
    return result;
}

如何上传文件到远程服务器?

收到的堆栈异常是:

at System.TimeSpan.Add(TimeSpan ts) 
at System.TimeSpan.op_Addition(TimeSpan t1, TimeSpan t2)
at WinSCP.Session.CheckForTimeout(String additional) 
at WinSCP.PatientFileStream.Wait(Int32& interval) 
at WinSCP.PatientFileStream.Read(Byte[] array, Int32 offset, Int32 count) 
at System.Xml.XmlTextReaderImpl.ReadData() 
at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars) 
at System.Xml.XmlTextReaderImpl.ParseText() 
at ProjectName.Upload(String HostName, String UName, String Password, String remotePath, String localFilePath)

【问题讨论】:

  • 请发布异常调用堆栈。
  • 在 System.TimeSpan.Add(TimeSpan ts) 在 System.TimeSpan.op_Addition(TimeSpan t1, TimeSpan t2) 在 WinSCP.Session.CheckForTimeout(附加字符串) 在 WinSCP.PatientFileStream.Wait(Int32& 间隔) 在 WinSCP.PatientFileStream.Read(Byte[] array, Int32 offset, Int32 count) 在 System.Xml.XmlTextReaderImpl.ReadData() 在 System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars) 在系统。 Xml.XmlTextReaderImpl.ParseText() at ProjectName.Upload(String HostName, String UName, String Password, String remotePath, String localFilePath)
  • 请将其编辑到您的问题中。它在评论中不可读。
  • 在最初的问题中更新。谢谢指出

标签: c# .net ftp winscp winscp-net


【解决方案1】:

SessionOptions.Timeout 设置为某个实际值:

Timeout = TimeSpan.FromDays(1),

【讨论】:

  • 谢谢。这解决了错误。代码完全执行,但如果我通过 putty 打开路径,我看不到文件。我有什么错误吗
猜你喜欢
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 2011-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-08
相关资源
最近更新 更多