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;
}
}
}
}
}
The
hostparameter of SSH.NETSftpClientconstructor, takes a host name (or an IP address), not any URL.So it should be like:
Similarly the
pathparameter ofSftpClient.DownloadFiletakes a path, not any URL.