Search code examples
c#.netftpwinscpwinscp-net

Files not getting transferred from Windows to Linux remote server


I am trying to use WinSCP in visual studio. I downloaded and installed WinSCP using the Managed NuGet package. I have used the below code in a web application to transfer one of the files to a remote Linux server. The code executes fine without any error, but the file is not transferred. I logged in using PuTTY to verify if the file has actually transferred, but could not locate the file. Below is the code used

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.FromDays(1),

        };

        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);
            }
            session.GetFiles(@"\\remoteserver\folder1\folder_backups\test_files\test1.txt", @"d:\folder3\").Check();
        }

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

The arguments are passed as below:

project1.Upload("remote host server", "username", "password", @"\\remote host server\folder1\folder_backups\test_files\", Fileupload1.PostedFile.FileName);

The code executes without any error, but no file is uploaded nor downloaded. How to fix this? Thanks


Solution

  • After the login happens in GUI - it points to /home/UserId . But the folder which i want to move the files exist in /folder1

    If remote path you want to use is /folder1/, use that for remotePath argument of your Upload method, instead of obviously wrong value @"\\remote host server\folder1\folder_backups\test_files\".

    project1.Upload("host", "user", "password", "/folder1/", Fileupload1.PostedFile.FileName);