4

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

Illep
  • 16,375
  • 46
  • 171
  • 302

3 Answers3

4

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.

Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
3

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());
            }
        }
Wigle
  • 96
  • 9
0

I'm doing this by using .GetFileList and reading the values into an ArrayList and then looping thru each value, adding the filename to a list box. I then check my input file against the list box to see if it exists. Sample code below to add the .GetFileList values into an ArrayList and then into a list box.

BTW - this is VB.NET :)

Dim InputFileList As ArrayList = oSftp.GetFileList(frmOptions.tbFTPInboundFolder.Text)
 For Each f In InputFileList
                If f.ToString() <> "." AndAlso f.ToString <> ".." Then
                    frmMain.lbFTPInputDirectory.Items.Add(f)
                End If
            Next
George Vaisey
  • 139
  • 1
  • 5
  • 19