Session.FileExists Method
Checks for existence of remote file or directory.
Advertisement
Syntax
C#
public bool FileExists(string path)
VB.NET
Public Function FileExists(path As String) As Boolean
Parameters
| Name | Description |
|---|---|
| string path | Full path to remote file.1 |
Return Value
true if file exists, false otherwise.
Exceptions
| Exception | Condition |
|---|---|
| InvalidOperationException | Session is not opened. |
| SessionLocalException | Error communicating with winscp.com. See the exception documentation for details. |
| TimeoutException | Timeout waiting for winscp.com to respond. |
Advertisement
Examples
C# Example
using System; using WinSCP; class Example { public static int Main() { try { // Setup session options SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Sftp, HostName = "example.com", UserName = "user", Password = "mypassword", SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." }; using (Session session = new Session()) { // Connect session.Open(sessionOptions); const string remotePath = "/home/user/test.txt"; if (session.FileExists(remotePath)) { Console.WriteLine("File {0} exists", remotePath); // Now you can e.g. download file using session.GetFiles return 0; } else { Console.WriteLine("File {0} does not exists", remotePath); return 1; } } } catch (Exception e) { Console.WriteLine("Error: {0}", e); return 2; } } }
Advertisement
VB.NET Example
Imports WinSCP Friend Class Example Public Shared Function Main() As Integer Try ' Setup session options Dim sessionOptions As New SessionOptions With sessionOptions .Protocol = Protocol.Sftp .HostName = "example.com" .UserName = "user" .Password = "mypassword" .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." End With Using session As New Session ' Connect session.Open(sessionOptions) Const remotePath As String = "/home/user/test.txt" If session.FileExists(remotePath) Then Console.WriteLine("File {0} exists", remotePath) ' Now you can e.g. download file using session.GetFiles Return 0 Else Console.WriteLine("File {0} does not exist", remotePath) Return 1 End If End Using Catch e As Exception Console.WriteLine("Error: {0}", e) Return 2 End Try End Function End Class
PowerShell Example
Learn more about using WinSCP .NET assembly from PowerShell.
Advertisement
try { # Load WinSCP .NET assembly Add-Type -Path "WinSCPnet.dll" # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = "example.com" UserName = "user" Password = "mypassword" SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." } $session = New-Object WinSCP.Session try