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

Author Message
Eduardo
[View user's profile]

Joined: 2011-01-05
Posts: 2
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:

Code:
' 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 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,
Eduardo
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
Have you read this FAQ?
http://winscp.net/eng/docs/faq_environment
_________________
Martin Prikryl
Eduardo
[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.

prikryl wrote:
Advertisements

You can post new topics in this forum






Search Site

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!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License