WinSCP .NET Assembly and COM Library

The WinSCP .NET assembly winscpnet.dll is a .NET wrapper around WinSCP’s scripting interface that allows your code to connect to a remote machine and manipulate remote files over SFTP, SCP, and FTP sessions from .NET languages, such as C#, VB.NET, and others, or from environments supporting .NET, such as PowerShell and SQL Server Integration Services (SSIS).

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 and VBScript, Visual Basic for Applications (VBA), Perl, and Python.

The library is primarily intended for advanced automation tasks that require conditional processing, loops or other control structures for which the basic scripting interface is too limited. The library is not a general purpose file transfer library. It particularly lacks support for interactive processing and as such it is not well suited for use in GUI applications.

Downloading and Installing the Assembly

First you need to download and install the assembly.

Advertisements:

Using Classes from WinSCP .NET Assembly

  1. Create an instance of the WinSCP.SessionOptions class and fill in all necessary information to allow an automatic connection and authentication of your session.
  2. Create an instance of the WinSCP.Session class. Optionally you can hook handlers of some events of the class.
  3. Open the session using 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.,

Classes

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.
FileTransferProgressEventArgs Provides data for file transfer progress event.
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.
TransferResumeSupport Configures automatic resume/transfer to temporary filename.

Examples

C# Example

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",
                SshHostKeyFingerprint = "ssh-rsa 2048 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;
        }
    }
}

VB.NET Example

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 mySessionOptions As New SessionOptions
            With mySessionOptions
                .Protocol = Protocol.Sftp
                .HostName = "example.com"
                .UserName = "user"
                .Password = "mypassword"
                .SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
            End With
 
            Using mySession As Session = New Session
                ' Connect
                mySession.Open(mySessionOptions)
 
                ' Upload files
                Dim myTransferOptions As New TransferOptions
                myTransferOptions.TransferMode = TransferMode.Binary
 
                Dim transferResult As TransferOperationResult
                transferResult = mySession.PutFiles("d:\toupload\*", "/home/user/", False, myTransferOptions)
 
                ' Throw on any error
                transferResult.Check()
 
                ' Print results
                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

PowerShell Example

JScript Example

VBScript Example

VBA Example

Perl Example

SSIS Example

Converting Script to Code Based on .NET Assembly

When you find yourself limited by scripting capabilities, you may consider converting your script to code that uses WinSCP .NET assembly.

License

The WinSCP .NET Assembly is free library: you can use it, redistribute it and/or modify it under the terms of the Mozilla Public License Version 2.0.

Advertisements:

Note that to use the assembly you need to have WinSCP installed. WinSCP uses different license, the GPL.

 
  library.txt · Last modified: by prikryl
 

Search Documentation

This page

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

Associations

Site design by Black Gate