WinSCP .NET Assembly winscp.dll is .NET wrapper around WinSCP scripting interface that allows connecting to a remote machine and manipulating remote files over SFTP, SCP or FTP session from .NET languages, like C#, VB.NET, PowerShell and others.
The assembly is also exposed to COM, and as such it can be used from variety of other programming languages and development environments, e.g. WSH-hosted active scripting languages, like JScript, VBScript; Visual Basic for Applications (VBA), Perl or Python.
This feature is available only in the latest beta release.
First, you need to download and install the assembly.
WinSCP.SessionOptions class and fill in all necessary information to allow an automatic connection and authentication of your session.WinSCP.Session class. Optionally you can hook handlers of some events of the class.Session.Open method, passing instance of your WinSCP.SessionOptions.
Once the session is opened, you can use any of the WinSCP.Session methods to manipulate remote files, e.g.,
Namespace: WinSCP
| Class | Description |
|---|---|
| ChmodEventArgs | Provides data for change of permissions event. |
| CommandExecutionResult | Represents results of Session.ExecuteCommand. |
| FailedEventArgs | Provides data for Session.Failed event. |
| FileOperationEventArgs | Provides data for abstract file operation event. |
| FilePermissions | Represents *nix-style remote file permissions. |
| OperationEventArgs | Provides data for abstract operation event. |
| OperationResultBase | Represents results of abstract batch operation. |
| RemoteDirectoryInfo | Represents data about remote directory. |
| RemoteFileInfo | Represents data about remote file. |
| RemovalEventArgs | Provides data for remote file removal event. |
| RemovalOperationResult | Represents results of file removal (Session.RemoveFiles). |
| Session | Represents session. Provides methods for manipulating remote files. |
| SessionException | Exception associated with the Session. |
| SessionLocalException | Exception associated with the Session originating from this assembly. |
| SessionOptions | Defines information to allow an automatic connection and authentication of the session. Is used with Session.Open method. |
| SessionRemoteException | Exception associated with the Session, originating from WinSCP console session. |
| SynchronizationResult | Represents results of synchronization (Session.SynchronizeDirectories). |
| TouchEventArgs | Provides data for remote file timestamp change event. |
| TransferEventArgs | Provides data for file transfer event. |
| TransferOperationResult | Represents results of file transfer (Session.GetFiles or Session.PutFiles). |
| TransferOptions | Defines options for file transfers. |
There are also other C# examples.
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", SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" }; using (Session session = new Session()) { // Connect session.Open(sessionOptions); // Upload files TransferOptions transferOptions = new TransferOptions(); transferOptions.TransferMode = TransferMode.Binary; TransferOperationResult transferResult; transferResult = session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions); // Throw on any error transferResult.Check(); // Print results foreach (TransferEventArgs transfer in transferResult.Transfers) { Console.WriteLine("Upload of {0} succeeded", transfer.FileName); } } return 0; } catch (Exception e) { Console.WriteLine("Error: {0}", e); return 1; } } }
There are also other VB.NET examples.
Imports System 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" .SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" End With Using session As Session = New Session ' Connect session.Open(sessionOptions) ' Upload files Dim transferOptions As New TransferOptions transferOptions.TransferMode = TransferMode.Binary Dim transferResult As TransferOperationResult transferResult = session.PutFiles("d:\toupload\*", "/home/user/", False, transferOptions) ' Throw on any error transferResult.Check ' Print results Dim transfer As TransferEventArgs For Each transfer In transferResult.Transfers Console.WriteLine("Upload of {0} succeeded", transfer.FileName) Next End Using Return 0 Catch e As Exception Console.WriteLine("Error: {0}", e) Return 1 End Try End Function End Class
There are also other PowerShell examples.
try { # Load WinSCP .NET assembly [Reflection.Assembly]::LoadFrom("WinSCP.dll") | Out-Null # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp $sessionOptions.HostName = "example.com" $sessionOptions.UserName = "user" $sessionOptions.Password = "mypassword" $sessionOptions.SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" $session = New-Object WinSCP.Session try { # Connect $session.Open($sessionOptions) # Upload files $transferOptions = New-Object WinSCP.TransferOptions $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary $transferResult = $session.PutFiles("b:\toupload\*", "./", $FALSE, $transferOptions) # Throw on any error $transferResult.Check() # Print results foreach ($transfer in $transferResult.Transfers) { Write-Host ("Upload of {0} succeeded" -f $transfer.FileName) } } finally { # Disconnect, clean up $session.Dispose() } exit 0 } catch [Exception] { Write-Host $_.Exception.Message exit 1 }
See overall VBA example.
See overall Perl example.
Site design by Black Gate