Differences

This shows you the differences between the selected revisions of the page.

2011-03-09 2011-03-25
using only two slashes for comment (martin) structured (martin)
Line 2: Line 2:
//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 operations. So WinSCP is not a library (e.g. .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 is not a library (e.g. .NET assembly) that you can call directly. Though this guides shows you how to use it seamlessly from the .NET code.
Before starting you should: Before starting you should:
Line 10: Line 10:
===== Using WinSCP from .NET Code ===== ===== Using WinSCP from .NET Code =====
 +==== [[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 ''[[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.
- 
-You can use standard input redirection (''[[http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardinput.aspx|ProcessStartInfo.RedirectStandardInput]]'') to feed [[script_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.))  
- 
-Redirection of standard output is less 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. 
- 
-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]]''). 
- 
<code csharp> <code csharp>
-const string logname = "log.xml"; 
- 
Process winscp = new Process(); Process winscp = new Process();
winscp.StartInfo.FileName = "winscp.com"; winscp.StartInfo.FileName = "winscp.com";
-winscp.StartInfo.Arguments = "/log=" + logname; 
winscp.StartInfo.UseShellExecute = false; winscp.StartInfo.UseShellExecute = false;
-winscp.StartInfo.RedirectStandardInput = true; 
winscp.StartInfo.CreateNoWindow = true; winscp.StartInfo.CreateNoWindow = true;
winscp.Start(); winscp.Start();
</code> </code>
 +
 +==== [[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 [[script_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 ''[[http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardinput.aspx|Process.StandardInput]]'' stream:
<code csharp> <code csharp>
 +winscp.StartInfo.RedirectStandardInput = true;
 +...
 +winscp.Start();
 +
winscp.StandardInput.WriteLine("option batch abort"); winscp.StandardInput.WriteLine("option batch abort");
winscp.StandardInput.WriteLine("option confirm off"); winscp.StandardInput.WriteLine("option confirm off");
Line 40: Line 37:
</code> </code>
-Now you need to wait for WinSCP to finish before you can safely start reading the log file+==== [[output]] Capturing outputs of WinSCP process ==== 
- +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.
-<code csharp> +
-winscp.StandardInput.Close()+
-winscp.WaitForExit(); +
-</code>+
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 (''[[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.
Line 55: Line 48:
</code> </code>
-Once WinSCP script finishes, you should [[guide_automation#results|check the results]]. First check exit code (''[[http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode.aspx|Process.ExitCode]]'') +==== [[log]] Using log file ===== 
-of the process:+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]]'').
<code csharp> <code csharp>
-if (winscp.ExitCode != 0) +const string logname = &quot;log.xml&quot;; 
-{ +... 
- ···// Error processing +winscp.StartInfo.Arguments = &quot;/log=" + logname; 
-} +... 
-else +winscp.Start();
-{ +
-····// Success processing +
-}+
</code> </code>
-To analyse results further, you may parse and interpret the XML log. First learn about [[logging_xml|XML logging]] to understand basic concepts.+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 wait for WinSCP to finish. 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]]'').
-The .NET library offers several classes for handling XML documents. The following example uses ''[[http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathdocument.aspx|System.Xml.XPath.XPathDocument]]'' class.+Following example shows how to use ''XPathDocument'':
<code csharp> <code csharp>
Line 89: Line 79:
</code> </code>
-In case of success, you can extract directory listing generated by ''[[script_commands#ls|ls]]'' command inside ''[[logging_xml#ls|ls]]'' element:+In case of success, you can e.g. extract directory listing generated by ''[[script_commands#ls|ls]]'' command inside ''[[logging_xml#ls|ls]]'' element:
<code csharp> <code csharp>
Line 100: Line 90:
</code> </code>
-===== Full C# Example =====+==== [[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.  
 + 
 +If you have output stream redirected, you need to first read it to the end. See [[guide_dotnet#output|above]]. 
 + 
 +A good practice is to close input stream too, if you have it redirected. 
 + 
 +<code csharp> 
 +// If input stream is redirected only 
 +winscp.StandardInput.Close(); 
 +// If output stream is redirected only 
 +string output = winscp.StandardOutput.ReadToEnd(); 
 +// Wait for process to completely shut down 
 +winscp.WaitForExit(); 
 +</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]]'') 
 +of the process: 
 + 
 +<code csharp> 
 +if (winscp.ExitCode != 0) 
 +
 +    // Error processing 
 +
 +else 
 +
 +    // Success processing 
 +
 +</code> 
 + 
 +===== [[csharp_example]] Full C# Example =====
Individual parts of this example are explained in the previous chapter. Individual parts of this example are explained in the previous chapter.
Line 169: Line 190:
</code> </code>
-===== Full VB.NET Example =====+===== [[vbnet_example]] Full VB.NET Example =====
Individual parts of this example are explained in the first chapter. Note that the VB.NET example was not tested. It is based on C# example above though, which was. Feel free to fix it. Individual parts of this example are explained in the first chapter. Note that the VB.NET example was not tested. It is based on C# example above though, which was. Feel free to fix it.

Last modified: by martin