I am trying to use SSH.NET to connect a linux server with SFTP connection to download file to a window directory.
Here is my code
string hostname = "1.1.1.1";
string username = "test";
string password = "test";
string localFilePath = @"C:test";
string NasFolderPath = “I have no idea about this path”;
string remoteDestinationPath = "";
LogWriter.LogWrite("Dest: "+ localFilePath + ", Source: " +NasFolderPath);
try
{
using (var client = new SshClient(hostname, 22, username, password))
{
client.Connect();
LogWriter.LogWrite("connectClient");
var sftp = new SftpClient(client.ConnectionInfo);
sftp.Connect();
LogWriter.LogWrite("ConnectSFTP");
var files = sftp.ListDirectory(NasFolderPath);
LogWriter.LogWrite("ConnectFolder" + files);
foreach (var file in files)
{
LogWriter.LogWrite(file.ToString());
if (!file.IsDirectory)
{
LogWriter.LogWrite("HaveDirectory");
using (var fileStream = System.IO.File.Create(Path.Combine(localFilePath, file.Name)))
{
LogWriter.LogWrite("TryToCopyFile");
sftp.DownloadFile(file.FullName, fileStream);
LogWriter.LogWrite("File downloaded and moved successfully.");
}
}
}
sftp.Disconnect();
// Download the file
////Upload the file
//using (var fileStream = File.OpenRead("localFilePath"))
//{
// client.UploadFile(fileStream, "remoteDestinationPath", true);
//}
////Optionally, delete the source file
//client.DeleteFile("remoteFilePath");
client.Disconnect();
}
}
catch (Exception ex)
{
LogWriter.LogWrite($"An error occurred: {ex.Message}");
}
The path to directory in the linux server is “volume1/TestFile”
I have tried to use
“sftp://[email protected]/volume1/TestFile”
“sftp://[email protected]/volume1/TestFile/”
“1.1.1.1/volume1/TestFile”
“1.1.1.1/volume1/TestFile/”
“sftp://[email protected]/TestFile”
“sftp://[email protected]/TestFile/”
“1.1.1.1/TestFile”
“1.1.1.1/TestFile/”
“1.1.1.1TestFile”
But seems all of this return with an error message “no such file”
What is the correct path for NasFolderPath?
or is there any other error of my code?