I am attempting to move file, yet nothing happens and no exceptions are reported.
public static void MoveFiles(string source, string destination, LoginInfo loginInfo)
{
using (SftpClient sftp = new SftpClient(loginInfo.Uri, loginInfo.Port, loginInfo.User, loginInfo.Password))
{
foreach (SftpFile file in sftp.ListDirectory(source))
{
file.MoveTo(destination + file.Name);
}
}
}
The debugger simply steps out of the foreach:

What am I doing wrong?
I am using the following dependencies:
using Renci.SshNet;
using Renci.SshNet.Sftp;
You need to connect the client to the server first using
sftp.Connect();
Source
And you should also wrap it in a try-catch in case of any errors.
public static void MoveFiles(string source, string destination, LoginInfo loginInfo) {
try {
using (SftpClient sftp = new SftpClient(loginInfo.Uri, loginInfo.Port, loginInfo.User, loginInfo.Password)) {
sftp.Connect();
var files = sftp.ListDirectory(source)
foreach (SftpFile file in files) {
file.MoveTo(destination + file.Name);
}
}
} catch(Exception ex) {
//...handle
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With