SftpClient.Exists and SftpClient.DownloadFile CREATE a file on server if file does not exist

Nov 14, 2012 at 8:49 PM

SftpClient.Exists always returns true, no matter if the file requested exists or not. If it does not and the login has write rights a zero byte file is created on the server.

 

SftpClient.DownloadFile will throw an exception if the file requsted does not exist, but only if the connected user doesn't have write rights. Otherwise it creates a 0 byte file and downloads it.

 

Get seems to work OK, unless you precede it with a Exists() call which makes a 0 byte file.

using (SftpClient sftp = new SftpClient("192.168.1.1", "user", "password"))
{
  sftp.Connect();
  sftp.ChangeDirectory("\\");
  string downloadedFileName = "test11.log";
  string localPath = "D:\\";

  // if (sftp.Exists(downloadedFileName))
  {
    using (var file = File.OpenWrite(localPath + downloadedFileName))
    {
     sftp.DownloadFile(downloadedFileName, file);
    }
  }
  sftp.Disconnect();
}

May 14, 2013 at 1:37 AM
Edited May 14, 2013 at 1:39 AM
Did you happen to resolve this? I'm having the same problem with the exists method. It always returns true, and changing directory into an invalid path doesn't throw an exception either.

I'm also using sysax multi server.
May 14, 2013 at 3:27 PM
We just don't call Exists(). Get() or DownloadFile() seem to work as expected.
May 14, 2013 at 3:32 PM
I managed to get it to work last night. In my case, for some reason, when using the pre-compiled .dll downloaded from here, it wasn't working. I ended up removing the reference to the DLL and re-referenced it to the project instead. Hoping that I could troubleshoot further with the project loaded. Once I re-compiled everything, the Exists() function works now.