Check if file exist on SFTP remote server

15,820

Solution 1

You could consider just taking the small hit and attempting to download the file. If it doesn't exist, an exception should be thrown and you can just catch it and move on. Checking for a file's existence is a volatile situation so in the majority of cases it's best to try and perform your action.

Solution 2

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());
            }
        }
Share:
15,820
Illep
Author by

Illep

I love writing code.

Updated on June 05, 2022

Comments

  • Illep
    Illep almost 2 years

    The following code will down 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 to Check if the file file.txt exist in the remote server or not. How can i do this check. Help

    I am using SharpSSH