"No known host" when connecting to SFTP server using SSH.NET

472 views Asked by At

I get the error:

no known host

I don't know why. But when I log in to FileZilla, I enter the same username and my login is accepted. Please help me with my code.

using System;
using Renci.SshNet;

namespace SftpFileZillaV3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            string host = "sftp://...";
            int port = 22;
            string username = "USERNAME";
            string password = "PASSWORD";
            string remoteFilePath = "sftp://C:.."; // Update with the correct remote file path
            string localFilePath = "C:\\Users\\tifti\\Desktop\\Deneme\\"; // Update with the desired local file path

            using (var sftp = new SftpClient(host, port, username, password))
            {
                try
                {
                    sftp.Connect();
                    if (sftp.IsConnected)
                    {
                        MemoryStream mem = new MemoryStream();
                        TextReader textReader = new StreamReader(mem);
                        sftp.DownloadFile(remoteFilePath, mem);
                        mem.Seek(0, SeekOrigin.Begin);
                        string s = textReader.ReadToEnd();
                        sftp.Disconnect();
                        label1.Text = "Download successful.";
                    }
                    else
                    {
                        label1.Text = "Connection unsuccessful!";
                    }
                }
                catch (Exception ex)
                {
                    label1.Text = "Error: " + ex.Message;
                }
            }
        }
    }
}
1

There are 1 answers

0
Martin Prikryl On

The host parameter of SSH.NET SftpClient constructor, takes a host name (or an IP address), not any URL.

So it should be like:

string host = "example.com";

Similarly the path parameter of SftpClient.DownloadFile takes a path, not any URL.