WinSCP does not work via code ASP.NET



WinSCP does not work via code ASP.NET


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 ( 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
        .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
        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)
        End With
        If Not output Is Nothing Then output = winscp.StandardOutput.ReadToEnd()

        ' Wait until WinSCP finishes

        ' 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", "")
        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 creacin 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,

Reply with quote


You can post new topics in this forum