Differences

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

2009-05-20 2009-05-22
created (martin) next part (martin)
Line 9: Line 9:
  * [[guide_automation|Know what WinSCP scripting commands to use for your task (e.g. file transfer)]].   * [[guide_automation|Know what WinSCP scripting commands to use for your task (e.g. file transfer)]].
-===== Example C# Code =====+===== Using WinSCP from .Net Code ===== 
 +To run ''[[executables|winscp.com]]'' use ''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'' (''StartInfo.FileName'') can be found (in current working directory or in search path), you need to provide full path otherwise. 
 + 
 +We can use standard input redirection (''StartInfo.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 it does not have any predefined form (cannot be parsed). But 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, we can use [[logging_xml|XML logging]]. For this we need to instruct WinSCP to stored log file using ''/log'' [[commandline|command-line parameter]] (''StartInfo.Arguments''). 
 + 
 +<code csharp> 
 +const string logname = "log.xml"; 
 + 
 +System.Diagnostics.Process winscp = new System.Diagnostics.Process(); 
 +winscp.StartInfo.FileName = "winscp.com"; 
 +winscp.StartInfo.Arguments = "/log=" + logname; 
 +winscp.StartInfo.UseShellExecute = false; 
 +winscp.StartInfo.RedirectStandardInput = true; 
 +winscp.StartInfo.CreateNoWindow = true; 
 +winscp.Start(); 
 +</code> 
 + 
 +To feed commands to standard input use ''StandardInput'' stream: 
 + 
 +<code csharp> 
 +winscp.StandardInput.WriteLine("option batch abort"); 
 +winscp.StandardInput.WriteLine("option confirm off"); 
 +winscp.StandardInput.WriteLine("open mysession"); 
 +winscp.StandardInput.WriteLine("ls"); 
 +winscp.StandardInput.WriteLine("put d:\\examplefile.txt"); 
 +</code> 
 + 
 +Now you need to wait for WinSCP to finish before you can safely start reading the log file: 
 + 
 +<code csharp> 
 +winscp.StandardInput.Close(); 
 +winscp.WaitForExit(); 
 +</code> 
 + 
 +If you want to collect the output, redirect the standard output before starting WinSCP (''RedirectStandardOutput'') and read from output stream (''StandardOutput''). You need to collect the output before calling ''WaitForExit''. The output stream has limited capacity. Once it gets filled, WinSCP hangs waiting for free space, never finishing. 
 + 
 +<code csharp> 
 +winscp.StartInfo.RedirectStandardOutput = true; 
 +... 
 +string output = winscp.StandardOutput.ReadToEnd(); 
 +</code> 
TODO TODO

Last modified: by martin