Search code examples
c#.netftpwinscpwinscp-net

WinSCP - Uploading files from Windows machine to Linux - Error: Timespan overflowed because the duration is too long


I installed WinSCP from Managed NuGet package.

I am trying to use WinSCP to upload files from Windows to Linux server. I am getting the error as

Timespan overflowed because the duration is too long.

I have tried the below code to upload the files.

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;
}

How to upload file to remote server?

Stack Exception received is:

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)

Solution

  • Set the SessionOptions.Timeout to some realistic value:

    Timeout = TimeSpan.FromDays(1),