Hello!
I am integrating WinCSP with ASP.NET but does not work, returns
ExitCode
= 1. I do not know why it happens because WinSCP works perfectly and also by command interface (winscp.com). I used the sample code for this integration:
' Run hidden WinSCP process
winscp = New Process()
logname = HttpContext.Current.Server.MapPath("../comun/incidencias/SFTP.xml")
With winscp.StartInfo
' SFTPExecutable needs to be defined in app.config to point to winscp.com
Try
.FileName = AppSettings("SFTPExecutable")
If .FileName Is Nothing OrElse .FileName.Length = 0 Then Throw (New Exception("from PutSFTP: SFTPExecutable not set in config file."))
Catch ex As Exception
errmsg = ex.Message
End Try
.Arguments = "/log=" + logname
.UseShellExecute = False
.RedirectStandardInput = True
.RedirectStandardOutput = True
.CreateNoWindow = False
End With
Try
winscp.Start()
Catch ex As Exception
errmsg = "Error SFTP: No se puede ejecutar WinSCP " & winscp.StartInfo.FileName & Environment.NewLine & ex.Message
End Try
' Feed in the scripting commands
With winscp.StandardInput
.WriteLine("option batch abort")
.WriteLine("option confirm off")
[color=red][b].WriteLine("open sftp://" & username & ":" & password & "@" & remotehost)[/b][/color]
.WriteLine("cd " & remoteDirectory)
.WriteLine("mkdir " & dirname)
.Close()
End With
If Not output Is Nothing Then output = 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)
If Not File.Exists(logname) Then
errmsg = "Error SFTP: Error en el ejecutable WinSCP."
Return False
End If
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?
Dim status As Boolean = (winscp.ExitCode = 0)
If Not status Then
errmsg = "Error SFTP: Error en creación de directorio " & dirname & "."
' See if there are any messages associated with the error
For Each message As XPathNavigator In nav.Select("//w:message", ns)
errmsg &= Environment.NewLine & message.Value
Next message
End If
Also on my development station works ok but on the server this code doesn't work. Thank you,
Eduardo