Topic "WinSCP does not work via code ASP.NET"

Author Message
[View user's profile]

Joined: 2011-01-05
Posts: 2

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,
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26890
Location: Prague, Czechia
Have you read this FAQ?
Martin Prikryl
[View user's profile]

Joined: 2011-01-05
Posts: 2
Thank you!

I have modified the "open" clause adding the SFTP fingerprint and after that I have changed security permissions on temp folder of the website (read-write permissions).

Now it works perfectly.

martin wrote:

You can post new topics in this forum


What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!


About donations

$9   $19   $49   $99

About donations


WinSCP Privacy Policy

WinSCP License