Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file exist on SFTP remote server

Tags:

c#

sftp

sharpssh

The following code will download the file named file.txt from the SFTP remote server to the local machine.

sftp.Get("/usr/mine/file.txt" , "C:/Folder/");

What I want to do is check if the file file.txt exist on the remote server or not. How can I do this check?

I am using SharpSSH

like image 415
Illep Avatar asked Oct 12 '25 19:10

Illep


1 Answers

This should do the trick.

using (var sftp = new SftpClient(host, username, password))
{
    try
    {

        sftp.Connect();
        MessageBox.Show(sftp.Exists(remoteDirectory).ToString());
    }
    catch (Exception Sftpex)
    {
        MessageBox.Show(Sftpex.ToString());
    }
}
like image 57
Wigle Avatar answered Oct 14 '25 11:10

Wigle