Differences
This shows you the differences between the selected revisions of the page.
guide_dotnet 2017-10-04 | guide_dotnet 2020-12-25 (current) | ||
Line 4: | Line 4: | ||
//This guide describes how to implement SFTP transfer in .NET application using WinSCP.// | //This guide describes how to implement SFTP transfer in .NET application using WinSCP.// | ||
- | WinSCP is SFTP client with [[scripting]] interface that you can use to automate many operations that it supports, including file transfers, synchronization and other. So WinSCP itself is not a library (e.g. [[library|.NET assembly]]) that you can call directly. Though this guides shows you how to use it seamlessly from the .NET code. | + | WinSCP is SFTP client with [[scripting]] interface that you can use to automate many operations that it supports, including file transfers, synchronization and other. So WinSCP itself is not a library (e.g. [[library|.NET assembly]]) that you can call directly. Though this guide shows you how to use it seamlessly from the .NET code. |
===== Before Starting ===== | ===== Before Starting ===== | ||
Line 14: | Line 14: | ||
===== Using WinSCP from .NET Code ===== | ===== Using WinSCP from .NET Code ===== | ||
+ | |||
==== [[running]] Running WinSCP Process ==== | ==== [[running]] Running WinSCP Process ==== | ||
To run ''[[executables|winscp.com]]'' use ''[[dotnet>system.diagnostics.process|System.Diagnostics.Process]]''. This class allows running any executable, possibly redirecting its standard input and output to a stream accessible from .NET code. Code below expects that ''winscp.com'' (''[[dotnet>system.diagnostics.processstartinfo.filename|ProcessStartInfo.FileName]]'') can be found in current working directory or in search path. You need to provide full path otherwise. | To run ''[[executables|winscp.com]]'' use ''[[dotnet>system.diagnostics.process|System.Diagnostics.Process]]''. This class allows running any executable, possibly redirecting its standard input and output to a stream accessible from .NET code. Code below expects that ''winscp.com'' (''[[dotnet>system.diagnostics.processstartinfo.filename|ProcessStartInfo.FileName]]'') can be found in current working directory or in search path. You need to provide full path otherwise. | ||
Line 44: | Line 45: | ||
While you can redirect standard output of WinSCP process, it is actually not very useful, as output of WinSCP does not have any predefined form (cannot be parsed). Though it can be useful to capture it, in case you want to show it to a user in your GUI or for diagnostic purposes. | While you can redirect standard output of WinSCP process, it is actually not very useful, as output of WinSCP does not have any predefined form (cannot be parsed). Though it can be useful to capture it, in case you want to show it to a user in your GUI or for diagnostic purposes. | ||
- | If you want to collect the output, redirect the standard output before starting WinSCP (''[[dotnet>system.diagnostics.processstartinfo.redirectstandardoutput|ProcessStartInfo.RedirectStandardOutput]]'') and read from output stream (''[[dotnet>system.diagnostics.process.standardoutput|Process.StandardOutput]]''). You need to continously collect the output while the script is running. The output stream has limited capacity. Once it gets filled, WinSCP hangs waiting for free space, never finishing. That means you cannot use ''[[dotnet>system.diagnostics.process.waitforexit|Process.WaitForExit]]'' on its own to wait for script to finish. Convenient alternative is ''[[dotnet>system.io.streamreader.readtoend|StreamReader.ReadToEnd]]'': | + | If you want to collect the output, redirect the standard output before starting WinSCP (''[[dotnet>system.diagnostics.processstartinfo.redirectstandardoutput|ProcessStartInfo.RedirectStandardOutput]]'') and read from output stream (''[[dotnet>system.diagnostics.process.standardoutput|Process.StandardOutput]]''). You need to continuously collect the output while the script is running. The output stream has limited capacity. Once it gets filled, WinSCP hangs waiting for free space, never finishing. That means you cannot use ''[[dotnet>system.diagnostics.process.waitforexit|Process.WaitForExit]]'' on its own to wait for script to finish. Convenient alternative is ''[[dotnet>system.io.streamreader.readtoend|StreamReader.ReadToEnd]]'': |
<code csharp> | <code csharp> | ||
winscp.StartInfo.RedirectStandardOutput = true; | winscp.StartInfo.RedirectStandardOutput = true; | ||
Line 95: | Line 96: | ||
</code> | </code> | ||
- | //See also guide to [[guide_interpreting_xml_log|interpreting XML log for advanced scripting]].// | + | //See also guide to [[guide_interpreting_xml_log|*]].// |
==== [[exit]] Waiting for script to complete ==== | ==== [[exit]] Waiting for script to complete ==== |