I know that I can upload and download files from/to a SFTP server using SftpClient
class of SSH.NET library but I am not sure how can this class be used for copying or moving a remote file on the SFTP server. I also did not find relevant material on internet. How can i copy or move a remote file from directory A to directory B, using SSH.NET library and C#.
Update:
I also tried experimenting with SshClient
class using the below code but it does nothing, neither any error nor any exception.
ConnectionInfo ConnNfo = new ConnectionInfo("FTPHost", 22, "FTPUser",
new AuthenticationMethod[]{
// Pasword based Authentication
new PasswordAuthenticationMethod("FTPUser","FTPPass")
}
);
using (var ssh = new SshClient(ConnNfo))
{
ssh.Connect();
if (ssh.IsConnected)
{
string comm = "pwd";
using (var cmd = ssh.CreateCommand(comm))
{
var returned = cmd.Execute();
var output = cmd.Result;
var err = cmd.Error;
var stat = cmd.ExitStatus;
}
}
ssh.Disconnect();
}
On Visual Studio console, i get the below output.
*SshNet.Logging Verbose: 1 : SendMessage to server 'ChannelRequestMessage': 'SSH_MSG_CHANNEL_REQUEST : #152199'.
SshNet.Logging Verbose: 1 : ReceiveMessage from server: 'ChannelFailureMessage': 'SSH_MSG_CHANNEL_FAILURE : #0'.*
In addition to SSH.NET's
SftpClient
, there is also the simplerScpClient
which worked when I ran intoSftpClient
issues.ScpClient
only has upload/download functionality, but that satisfied my use case.Uploading:
Downloading:
Moving a remote File can be done using SSH.NET library. Available here: https://github.com/sshnet/SSH.NET
Here is sample code to move first file from one source folder to another. Set private variables in the class according to the FTP set up.
With the NuGet package SSH.NET by Renci I use the following:
To Move a file:
To Copy a file: