Renci.SshNet.SftpClient.Exists C# (CSharp) Method

Exists() public method

Checks whether file or directory exists;
is null or contains only whitespace characters. Client is not connected. Permission to perform the operation was denied by the remote host. -or- A SSH command was denied by the server. A SSH error where is the message from the remote host. The method was called after the client was disposed.
public Exists ( string path ) : bool
path string The path.
return bool
        public bool Exists(string path)
        {
            CheckDisposed();

            if (path.IsNullOrWhiteSpace())
                throw new ArgumentException("path");

            if (_sftpSession == null)
                throw new SshConnectionException("Client not connected.");

            var fullPath = _sftpSession.GetCanonicalPath(path);

            // using SSH_FXP_REALPATH is not an alternative the SFTP specification has not always
            // been clear on how the server should respond when the specified path is not present
            // on the server:
            // 
            // SSH 1 to 4:
            // No mention of how the server should respond if the path is not present on the server.
            //
            // SSH 5:
            // The server SHOULD fail the request if the path is not present on the server.
            // 
            // SSH 6:
            // Draft 06: The server SHOULD fail the request if the path is not present on the server.
            // Draft 07 to 13: The server MUST NOT fail the request if the path does not exist.
            //
            // Note that SSH 6 (draft 06 and forward) allows for more control options, but we
            // currently only support up to v3.

            try
            {
                _sftpSession.RequestLStat(fullPath);
                return true;
            }
            catch (SftpPathNotFoundException)
            {
                return false;
            }
        }

Usage Example

 public bool UploadFile(string filePath)
 {
     ConnectionInfo connectionInfo = new PasswordConnectionInfo(_address, ConstFields.SFTP_PORT, _username, _password);
     try
     {
         using (var sftp = new SftpClient(connectionInfo))
         {
             sftp.Connect();
             using (var file = File.OpenRead(filePath))
             {
                 if (!sftp.Exists(ConstFields.TEMP_PRINT_DIRECTORY))
                 {
                     sftp.CreateDirectory(ConstFields.TEMP_PRINT_DIRECTORY);
                 }
                 sftp.ChangeDirectory(ConstFields.TEMP_PRINT_DIRECTORY);
                 string filename = Path.GetFileName(filePath);
                 sftp.UploadFile(file, filename);
             }
             sftp.Disconnect();
         }
     }
     catch (Renci.SshNet.Common.SshConnectionException)
     {
         Console.WriteLine("Cannot connect to the server.");
         return false;
     }
     catch (System.Net.Sockets.SocketException)
     {
         Console.WriteLine("Unable to establish the socket.");
         return false;
     }
     catch (Renci.SshNet.Common.SshAuthenticationException)
     {
         Console.WriteLine("Authentication of SSH session failed.");
         return false;
     }
     return true;
 }
All Usage Examples Of Renci.SshNet.SftpClient::Exists