Differences
This shows you the differences between the selected revisions of the page.
2011-07-31 | 2011-12-31 | ||
scripting#commands (martin) | dotnet interwiki (martin) | ||
Line 11: | Line 11: | ||
===== Using WinSCP from .NET Code ===== | ===== Using WinSCP from .NET Code ===== | ||
==== [[running]] Running WinSCP Process ==== | ==== [[running]] Running WinSCP Process ==== | ||
- | To run ''[[executables|winscp.com]]'' use ''[[http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx|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'' (''[[http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.filename.aspx|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. |
<code csharp> | <code csharp> | ||
Process winscp = new Process(); | Process winscp = new Process(); | ||
Line 21: | Line 21: | ||
==== [[input]] Feeding scripting commands using standard input ==== | ==== [[input]] Feeding scripting commands using standard input ==== | ||
- | You can use standard input redirection (''[[http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardinput.aspx|ProcessStartInfo.RedirectStandardInput]]'') to feed [[scripting#commands|scripting commands]], sparing necessity to assemble temporary script file.((Of course unless what you plan to do is actually execution of existing script file.)) | + | You can use standard input redirection (''[[dotnet>system.diagnostics.processstartinfo.redirectstandardinput|ProcessStartInfo.RedirectStandardInput]]'') to feed [[scripting#commands|scripting commands]], sparing necessity to assemble temporary script file.((Of course unless what you plan to do is actually execution of existing script file.)) |
- | To feed commands to standard input use ''[[http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardinput.aspx|Process.StandardInput]]'' stream: | + | To feed commands to standard input use ''[[dotnet>system.diagnostics.process.standardinput|Process.StandardInput]]'' stream: |
<code csharp> | <code csharp> | ||
Line 40: | Line 40: | ||
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 (''[[http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx|ProcessStartInfo.RedirectStandardOutput]]'') and read from output stream (''[[http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx|Process.StandardOutput]]''). You need to collect the output before calling ''[[http://msdn.microsoft.com/en-us/library/system.diagnostics.process.waitforexit.aspx|Process.WaitForExit]]''. The output stream has limited capacity. Once it gets filled, WinSCP hangs waiting for free space, never finishing. | + | 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 collect the output before calling ''[[dotnet>system.diagnostics.process.waitforexit|Process.WaitForExit]]''. The output stream has limited capacity. Once it gets filled, WinSCP hangs waiting for free space, never finishing. |
<code csharp> | <code csharp> | ||
Line 49: | Line 49: | ||
==== [[log]] Using log file ===== | ==== [[log]] Using log file ===== | ||
- | To capture results of script, you can use [[logging_xml|XML logging]]. For this you need to instruct WinSCP to store log file using ''/log'' [[commandline|command-line parameter]] (''[[http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.arguments.aspx|ProcessStartInfo.Arguments]]''). | + | To capture results of script, you can use [[logging_xml|XML logging]]. For this you need to instruct WinSCP to store log file using ''/log'' [[commandline|command-line parameter]] (''[[dotnet>system.diagnostics.processstartinfo.arguments|ProcessStartInfo.Arguments]]''). |
<code csharp> | <code csharp> | ||
Line 59: | Line 59: | ||
</code> | </code> | ||
- | Note that before you can safely start reading and parsing the XML log file using tree-based parser (such as ''[[http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx|XmlDocument]]'' or ''[[http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathdocument.aspx|XPathDocument]]''), you need to [[guide_dotnet#exit|wait for WinSCP to finish]]. See example below. | + | Note that before you can safely start reading and parsing the XML log file using tree-based parser (such as ''[[dotnet>system.xml.xmldocument|XmlDocument]]'' or ''[[dotnet>system.xml.xpath.xpathdocument|XPathDocument]]''), you need to [[guide_dotnet#exit|wait for WinSCP to finish]]. See example below. |
- | If you need to read the log file continuously, you need to use stream-based parser (such as ''[[http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx|XmlReader]]''). See [[guide_interpreting_xml_log#continuous|example]]. | + | If you need to read the log file continuously, you need to use stream-based parser (such as ''[[dotnet>system.xml.xmlreader|XmlReader]]''). See [[guide_interpreting_xml_log#continuous|example]]. |
Following example shows how to use tree-based parsing using ''XPathDocument'': | Following example shows how to use tree-based parsing using ''XPathDocument'': | ||
Line 95: | Line 95: | ||
==== [[exit]] Waiting for script to complete ==== | ==== [[exit]] Waiting for script to complete ==== | ||
- | Use ''[[http://msdn.microsoft.com/en-us/library/system.diagnostics.process.waitforexit.aspx|Process.WaitForExit]]'' to wait for WinSCP process to finish. | + | Use ''[[dotnet>system.diagnostics.process.waitforexit|Process.WaitForExit]]'' to wait for WinSCP process to finish. |
If you have output stream redirected, you need to first [[guide_dotnet#output|read the output stream to the end]]. | If you have output stream redirected, you need to first [[guide_dotnet#output|read the output stream to the end]]. | ||
Line 111: | Line 111: | ||
==== [[exitcode]] Checking exit code ==== | ==== [[exitcode]] Checking exit code ==== | ||
- | Once WinSCP script finishes, check exit code (''[[http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode.aspx|Process.ExitCode]]'') | + | Once WinSCP script finishes, check exit code (''[[dotnet>system.diagnostics.process.exitcode|Process.ExitCode]]'') |
of the process: | of the process: | ||