This is an old revision of the document!
Converting Script to Code Based on .NET Assembly
When you have your automation task implemented using WinSCP script, you may sooner or later find yourself limited by its capabilities. The scripting particularly lacks (by design) any support for control structures (like conditional processing, loops/cycles, etc.). A solution is to convert your script to code that uses WinSCP .NET assembly.
Advertisement
Choosing Language
Start by choosing a language. WinSCP .NET assembly can be used from any .NET language or any language that supports COM.
If you do not have your own preferred language, use Windows PowerShell. PowerShell is scripting language built on top of .NET framework. It comes with Windows 7 and newer and can be installed into Windows XP and Vista. PowerShell scripts can be directly executed, they do not need to be compiled first. See example PowerShell script that uses WinSCP .NET assembly.
Mapping Script Commands to .NET Assembly Calls
Most script commands can be directly mapped to their equivalent .NET assembly method calls. The mapping is described in respective scripting command documentation page.
There are some conceptual differences though. These are discussed in following sections.
Interactive/Batch Mode
Scripts run in an interactive mode by default (option batch off
). There’s no equivalent for this in .NET assembly, which always run in batch mode. It is near equivalent to option batch abort
in scripting mode, what is recommended setting, as used in most scripting examples. Read mode about capturing errors in .NET assembly.
Default Configuration
Scripting mode by default shares configuration with graphical mode. On the contrary the .NET assembly is by default isolated from graphical mode configuration (equivalent to using /ini=nul
command-line parameter in scripting mode).
Advertisement
It means that you cannot use sites/stored sessions, when opening session with .NET assembly. You need to configure all your session settings directly in your code (using SessionOptions
class).
It also means that with .NET assembly, you always start with default transfer settings, default resume/endurance settings, etc.
Alternatively, you can force the .NET assembly to share configuration with graphical/scripting mode, by setting Session.DefaultConfiguration
to false
. Though this is not recommended practise.
Relative/Absolute Paths
Scripting mode (similarly to graphical mode) has concept of current working directory (both on local and remote side). On the contrary, the .NET assembly does not; there’s no equivalent to cd
command.
In the scripting you can use paths relative to current working directory in parameters to script commands. In the .NET assembly, you need to always use absolute paths.
For example following script snippet:
cd /home/martinp lcd d:\ get test.txt
needs to be converted to following call:
$session.GetFiles("/home/martinp/test.txt", "d:\")