Code to download file from FTP with WinSCP .NET assembly does not connect, but I can connect in WinSCP GUI
Asked Answered
C

2

5

I need to download a file from FTP thru WinSCP .NET assembly. I have this code currently but the error says Authentication failed.

try
{
    SessionOptions sessionOptions = new SessionOptions
    {
        Protocol = Protocol.Ftp,
        HostName = "172.xx.xxx.xx",
        UserName = "usersample",
        Password = "P@ssw0rd",
        PortNumber = 21
    };

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

        // Download files
        TransferOptions transferOptions = new TransferOptions();
        transferOptions.TransferMode = TransferMode.Binary;

        TransferOperationResult transferResult;
        transferResult =
            session.GetFiles(
                "/HST/sample.txt", "C:\\Users\\john\\Documents\\SampleFolder\\",
                false, transferOptions);

        // Throw on any error
        transferResult.Check();

        // Print results
        foreach (TransferEventArgs transfer in transferResult.Transfers)
        {
            Console.WriteLine("Download of {0} succeeded", transfer.FileName);
        }

        Console.ReadLine();
    }

}
catch (Exception e)
{
    Console.WriteLine("Error: {0}", e);
    Console.ReadLine();
}

I got the code from this reference: https://winscp.net/eng/docs/library_session_getfiles

Originally, it has the following:

SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"

But I removed it since I don't have it and I think it's for SFTP (not sure) I've tried the credentials manually and they work. I am able to login. Please help. Thank you.

Ceresin answered 17/8, 2017 at 3:21 Comment(0)
J
7

There's WinSCP FAQ for your kind of situation:
Why I cannot connect/transfer using script, when I can using GUI (or vice versa)?


The easiest solution, to start with, is to use Generate transfer code function from your GUI session, to get a working code template.

  • Login in GUI;
  • Select the file to download in remote panel and the target local directory in local panel;
  • Use Download command;
  • On the Download transfer options dialog, use Transfer settings > Generate Code;
  • Generate transfer code dialog opens. Select the .NET assembly tab and make sure C# language is selected.

    enter image description here

Jigaboo answered 17/8, 2017 at 4:46 Comment(1)
OMG. You just saved my life. Thank you very much. I followed your precised instructions and it's working now.Ceresin
S
0

You need SSH host key fingerprint in addition to your credentials for SSH connection.

You should get an SSH host key fingerprint along with your credentials from a server administrator. Knowing the host key fingerprint and thus being able to verify it is an integral part of securing an SSH connection. It prevents man-in-the-middle attacks.

This may help you to get your Host-Key:

https://winscp.net/eng/docs/faq_hostkey

https://winscp.net/eng/docs/ui_fsinfo

Staccato answered 17/8, 2017 at 3:40 Comment(1)
The OP is using FTP, not SSH (SFTP).Jigaboo

© 2022 - 2024 — McMap. All rights reserved.