Differences

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

2009-05-25 2009-05-25
almost dome except for VB.NET example (martin) vb.net example (martin)
Line 157: Line 157:
else else
{ {
-    // It can be worth looking for directory listing even in case of +    /// It can be worth looking for directory listing even in case of 
-    // error as possibly only upload may fail+    /// error as possibly only upload may fail
       
    XPathNodeIterator files = nav.Select("//w:file", ns);     XPathNodeIterator files = nav.Select("//w:file", ns);
Line 170: Line 170:
===== Full VB.NET Example ===== ===== Full VB.NET Example =====
-TODO+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. 
 + 
 +<code vbnet> 
 +Imports System 
 +Imports System.Diagnostics 
 +Imports System.Xml 
 +Imports System.Xml.XPath 
 + 
 +... 
 + 
 +Const logname As String = "log.xml" 
 + 
 +' Run hidden WinSCP process 
 +Dim winscp As Process = New Process() 
 +winscp.StartInfo.FileName = "winscp.com" 
 +winscp.StartInfo.Arguments = "/log=" + logname 
 +winscp.StartInfo.UseShellExecute = False 
 +winscp.StartInfo.RedirectStandardInput = True 
 +winscp.StartInfo.RedirectStandardOutput = True 
 +winscp.StartInfo.CreateNoWindow = True 
 +winscp.Start() 
 + 
 +' Open the session 
 +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") 
 +winscp.StandardInput.Close() 
 + 
 +' Collect all output (not used in this example) 
 +Dim output As String = winscp.StandardOutput.ReadToEnd() 
 + 
 +' Wait until WinSCP finishes 
 +winscp.WaitForExit() 
 + 
 +' Parse and interpret the XML log 
 +' (Note that in case of fatal failure the log file may not exist at all) 
 +Dim log As XPathDocument = New XPathDocument(logname) 
 +Dim ns As XmlNamespaceManager = New XmlNamespaceManager(New NameTable()) 
 +ns.AddNamespace("w", "http://winscp.net/schema/session/1.0") 
 +Dim nav As XPathNavigator = log.CreateNavigator() 
 + 
 +' Success (0) or error? 
 +If winscp.ExitCode <> 0 Then 
 + 
 +    Console.WriteLine("Error occured") 
 + 
 +    ' See if there are any messages associated with the error 
 +    For Each message As XPathNavigator In nav.Select("//w:message", ns) 
 +        Console.WriteLine(message.Value) 
 +    Next 
 + 
 +Else 
 + 
 +    ' It can be worth looking for directory listing even in case of 
 +    ' error as possibly only upload may fail 
 +     
 +    Dim files As XPathNodeIterator = nav.Select("//w:file", ns) 
 +    Console.WriteLine(string.Format("There are {0} files and subdirectories:", files.Count)) 
 +    For Each file As XPathNavigator In files 
 +        Console.WriteLine(file.SelectSingleNode("w:filename/@value", ns).Value) 
 +    Next 
 + 
 +End If 
 +</code> 

Last modified: by martin