WinSCP-将文件从Windows计算机上传到Linux-错误:由于持续时间太长,时间跨度溢出

时间:2019-01-09 13:30:11

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

我从Managed 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)

1 个答案:

答案 0 :(得分:1)

SessionOptions.Timeout设置为一些实际值:

Timeout = TimeSpan.FromDays(1),