This is an old revision of the document!
Session Class
This is the main interface class of the WinSCP assembly.
It represents a session and provides methods for manipulating remote files over SFTP, SCP or FTP session.
Advertisement
This feature is available only in the latest beta release.
Syntax
Namespace: WinSCP
public sealed class Session : IDisposable
Public NotInheritable Class Session Implements IDisposable
Constructors
| Name | Description | 
|---|---|
| Session() | Default constructor. | 
Advertisement
Properties
| Name | Description | 
|---|---|
| string AdditionalExecutableArguments | Additional command-line arguments to be passed to winscp.com. In general, this should be left with defaultnull. | 
| string DebugLogPath | Path to store assembly debug log to. Default nullmeans, no debug log file is created. See alsoSessionLogPath. | 
| bool DefaultConfiguration | Should WinSCP be forced with the default configuration? trueby default. Useful to isolate the console session run from any configuration stored on this machine. Set tofalseonly in an exceptional scenarios. | 
| bool DisableVersionCheck | Disables test that WinSCP executables have the same product version as this assembly.1 | 
| string ExecutablePath | Path to winscp.exe. The default isnull, meaning thatwinscp.exeis looked for in the same directory as this assembly or in an installation folder. | 
| string IniFilePath | Path to an INI file. Used only when DefaultConfigurationisfalse. Whennull, default WinSCP configuration storage is used, meaning INI file, if any is present in WinSCP startup directory, or Windows Registry otherwise. | 
| string SessionLogPath | Path to store session log file to. Default nullmeans, no session log file is created. See alsoDebugLogPath. | 
| bool Opened | Is session opened yet? true, whenOpenwas successfully called already. Read-only. | 
| StringCollection Output | Output of WinSCP console session. See also OutputDataReceivedevent. Read-only. | 
| TimeSpan Timeout | Maximal interval between two consecutive outputs from WinSCP console session, before TimeoutException is thrown. The default is one minute. | 
Methods
| Name | Description | 
|---|---|
| Abort | Forcefully aborts session. | 
| CreateDirectory | Creates remote directory. | 
| Dispose | Closes session, terminates underlying WinSCP process. | 
| ExecuteCommand | Executes command on the remote server. | 
| FileExists | Checks for existence of remote file. | 
| GetFileInfo | Retrieves information about remote file. | 
| GetFiles | Downloads files. | 
| ListDirectory | Lists remote directory. | 
| MoveFile | Moves remote file to another remote directory and/or renames remote file. | 
| Open | Opens the session. | 
| PutFiles | Uploads files. | 
| RemoveFiles | Removes remote files. | 
| SynchronizeDirectories | Synchronizes local directory with remote directory. | 
Advertisement
Events
| Name | Description | 
|---|---|
| Failed | Occurs on error during any operation. | 
| FileTransferred | Occurs when file is transfered. | 
| OutputDataReceived | Occurs on output from WinSCP console session. | 
Remarks
To use the class:
- Create an instance of SessionOptionsand fill in all needed information to connect and authenticate the session automatically;
- Create an instance of Sessionclass;
- Usually all properties of Sessioncan be left with their default values;
- Assign event handlers, if continuous monitoring of operation is required;
- Call Openmethod passing instance ofSessionOptions;
- Use any file manipulation methods you need, such as GetFiles,PutFiles,SynchronizeDirectories.
The class is locked against concurrent accesses from multiple threads. Though you can use several instances of the class in paralel, even from different threads.
Capturing Results of Operations
There are two classes of operations, hence two classes of methods; atomic operations, such as Open, ListDirectory, ExecuteCommand, etc; and batch operations, such as GetFiles, PutFiles, SynchronizeDirectories, etc.
Methods from both classes throw SessionLocalException on error in communication with WinSCP console session.
Methods for atomic operations throw SessionRemoteException on error originating from WinSCP console session (refered to as “failure” below).
Methods for batch operations returns an instance of descendant of OperationResultBase class (such as TransferOperationResult or SynchronizationResult). Such result class stores list of operations performed (e.g. TransferOperationResult.Uploads), and list of failures (OperationResultBase.Failures).
Every structure representing operation performed may refer (e.g. in TransferEventArgs.Error) to one of the failures, if the failure can be explicitly associated with the operation. So often the same failure (represented by SessionRemoteException) will be referenced twice in the results.
But there can be failures that cannot be explicitly associated with any operation represented in results. An example is an error when listing contents of remote directory to determine list of files to downloads. The listing is not represented in the results, so the failure will be included only in a generic list of failures.
Advertisement
If you do not want to check the list of failures after every batch operation method call, you can use method OperationResultBase.Check to throw the first failure in the list, if any failure occured:
// Throw, if upload fails session.PutFiles(localPath, remotePath).Check();
Also for all failures the Session.Failed event is raised.
Example
See overall example for WinSCP .NET assembly or any other example.
- Note, even if you set this property, you need to use the assembly with compatible WinSCP executable only. Otherwise the behaviour is unpredictable. In general, it is not recommended to use this. It neither case you will be able to use the assembly with WinSCP 5.0.4 or older.Back