Differences

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

2014-03-06 2014-03-24
no summary (rtek1000) reverting alternative VB.net code as I got no explanation in topics 13603 and 13598 what does it show (martin)
Line 600: Line 600:
===== [[vbnet_robust_example]] Running WinSCP from VB.NET  ===== ===== [[vbnet_robust_example]] Running WinSCP from VB.NET  =====
&deprecated_use_net &deprecated_use_net
- 
-__**Please note the existence of a second code. This code is functional for uploading and donwload with vb .net 2005. This same code that worked in VB . Net 2005 was also tested in VB .Net 2012 (Version 11.0.50.727.42 & Netframework Version 4.5.50938) and worked without changes**__ 
Following is more robust alternative to simple example contained in guide to [[guide_dotnet|SFTP file transfers in .NET]]. Following is more robust alternative to simple example contained in guide to [[guide_dotnet|SFTP file transfers in .NET]].
Line 688: Line 686:
    End Function     End Function
End Class End Class
-</code> 
- 
-In this code below, have to add the files in there resources tab in the project properties, add also the OpenFileDialog1 and SaveFileDialog1 object. Add WinSCP.com and WinSCP.exe files as resources. The reference: Public Class WINSCP is for the form class 
-The Full Project can be download at link forum: 
-winscp.net/forum/viewtopic.php?p=49701#49701 
- 
-This code that work in VB .Net 2005 and also tested in VB .Net 2012 and worked without changes. (W7 x64 RAM: 2GB) 
- 
-<code vbnet> 
-Imports System.IO 
-Imports System.Diagnostics 
-Imports System 
-Imports System.Xml 
-Imports System.Xml.XPath 
- 
-Public Class WINSCP 
- 
-    'field for storing the command for opening connections. 
-    Private _connect As String = "" 
- 
-    Public Sub New_connection(ByVal host As String, ByVal username As String, _ 
-            ByVal password As String, Optional ByVal port As Integer = 22) 
- 
-        'get the full path to the systems temp folder for storing 
-        'the WinSCP.com and WinSCP.exe files in. 
-        Dim tempPath As String = Path.GetTempPath 
- 
-        'check to see if the WinSCP files are in the temp path. 
-        'if they are not then copy them from the resources. 
-        If File.Exists(tempPath & "WinSCP.com") = False Then 
-            File.WriteAllBytes(tempPath & "WinSCP.com", My.Resources.WinSCP) 
-        End If 
-        If File.Exists(tempPath & "WinSCP.exe") = False Then 
-            File.WriteAllBytes(tempPath & "WinSCP.exe", My.Resources.WinSCP1) 
-        End If 
- 
-        'attempt to get the host key from the SSH server. This 
-        'is required to make a connection to the server without storing it 
-        'in the winscp.ini file. 
-        Dim hostKey = GetAndStoreFingerprint(host & ":" & port.ToString) 
- 
-        'build connection string for opening ssh connections. 
-        'this command needs to be run before any other commands. 
-        _connect = "open sftp://" & username & ":" & password _ 
-          & "@" & host & ":" & port.ToString _ 
-          & " -hostKey=" & """" & hostKey & """" 
- 
-    End Sub 
- 
-    Public Function ListFile() As Boolean 
- 
-        Try 
- 
-            'create the required startinfo for WinSCP to run in background. 
-            Dim startInfo As New ProcessStartInfo 
-            startInfo.FileName = Path.GetTempPath & "WinSCP.com" 
-            startInfo.RedirectStandardInput = True 
-            startInfo.RedirectStandardOutput = True 
-            startInfo.UseShellExecute = False 
-            startInfo.CreateNoWindow = True 
-            startInfo.WindowStyle = ProcessWindowStyle.Hidden 
- 
-            'create the process object. 
-            Dim process As New Process 
-            process.StartInfo = startInfo 
-            process.Start() 
- 
-            'run the commands  
-            process.StandardInput.WriteLine(_connect) 
-            If CheckBox1.Checked = True Then 
-                process.StandardInput.WriteLine("option confirm off") 
-            End If 
- 
-            process.StandardInput.WriteLine("ls") 
- 
-            process.StandardInput.WriteLine("close") 
-            process.StandardInput.WriteLine("exit") 
-            process.StandardInput.Close() 
- 
-            'get the response 
-            Dim response As String = process.StandardOutput.ReadToEnd 
-            process.WaitForExit() 
-            TextBox1.Text = response 
- 
-            TextBox1.SelectionLength = 0 
- 
-            TextBox1.SelectionStart = TextBox1.Text.Length 
- 
-            TextBox1.ScrollToCaret() 
- 
- 
-            'check the result. 
-            Dim exitCode = process.ExitCode 
-            If exitCode = 0 Then 
-                Return True 
-            Else 
-                'find out why failed. 
-            End If 
- 
- 
-        Catch ex As Exception 
-            Return False 
-        End Try 
- 
-        'failed because exitCode should be 0 
-        Return False 
-    End Function 
-    Public Function UploadFile(ByVal localFilePath As String, _ 
-                    ByVal remoteFilePath As String) As Boolean 
- 
-        'wrap paths in quotes. 
-        localFilePath = """" & localFilePath & """" 
-        remoteFilePath = """" & remoteFilePath & """" 
- 
-        Try 
- 
-            'create the required startinfo for WinSCP to run in background. 
-            Dim startInfo As New ProcessStartInfo 
-            startInfo.FileName = Path.GetTempPath & "WinSCP.com" 
-            startInfo.RedirectStandardInput = True 
-            startInfo.RedirectStandardOutput = True 
-            startInfo.UseShellExecute = False 
-            startInfo.CreateNoWindow = True 
-            startInfo.WindowStyle = ProcessWindowStyle.Hidden 
- 
-            'create the process object. 
-            Dim process As New Process 
-            process.StartInfo = startInfo 
-            process.Start() 
- 
-            'run the commands  
-            process.StandardInput.WriteLine(_connect) 
-            If CheckBox1.Checked = True Then 
-                process.StandardInput.WriteLine("option confirm off") 
-            End If 
-            process.StandardInput.WriteLine("put " & remoteFilePath & " " & localFilePath) 
-            process.StandardInput.WriteLine("close") 
-            process.StandardInput.WriteLine("exit") 
-            process.StandardInput.Close() 
- 
-            'get the response 
-            Dim response As String = process.StandardOutput.ReadToEnd 
-            process.WaitForExit() 
-            TextBox1.Text = response 
- 
-            TextBox1.SelectionLength = 0 
- 
-            TextBox1.SelectionStart = TextBox1.Text.Length 
- 
-            TextBox1.ScrollToCaret() 
- 
- 
-            'check the result. 
-            Dim exitCode = process.ExitCode 
-            If exitCode = 0 Then 
-                Return True 
-            Else 
-                'find out why failed. 
-            End If 
- 
- 
-        Catch ex As Exception 
-            Return False 
-        End Try 
- 
-        'failed because exitCode should be 0 
-        Return False 
-    End Function 
- 
-    Public Function DownloadFile(ByVal remoteFilePath As String, _ 
-                  ByVal localFilePath As String) As Boolean 
- 
-        'wrap paths in quotes. 
-        localFilePath = """" & localFilePath & """" 
-        remoteFilePath = """" & remoteFilePath & """" 
- 
-        Try 
- 
-            'create the required startinfo for program to run in background. 
-            Dim startInfo As New ProcessStartInfo 
-            startInfo.FileName = Path.GetTempPath & "WinSCP.com" 
-            startInfo.RedirectStandardInput = True 
-            startInfo.RedirectStandardOutput = True 
-            startInfo.UseShellExecute = False 
-            startInfo.CreateNoWindow = True 
-            startInfo.WindowStyle = ProcessWindowStyle.Hidden 
- 
-            'create the process object. 
-            Dim process As New Process 
-            process.StartInfo = startInfo 
-            process.Start() 
- 
-            'run the commands 
-            process.StandardInput.WriteLine(_connect) 
-            process.StandardInput.WriteLine("get " & remoteFilePath & " " & localFilePath) 
-            process.StandardInput.WriteLine("close") 
-            process.StandardInput.WriteLine("exit") 
-            process.StandardInput.Close() 
- 
-            'get the response 
-            Dim response As String = process.StandardOutput.ReadToEnd 
-            process.WaitForExit() 
-            TextBox1.Text = response 
- 
-            'check the result. 
-            Dim exitCode = process.ExitCode 
-            If exitCode = 0 Then 
-                Return True 
-            Else 
-                'find out why failed. 
-            End If 
- 
-        Catch ex As Exception 
-            Return False 
-        End Try 
- 
-        'failed because exitCode should be 0 
-        Return False 
- 
-    End Function 
- 
-    Private Function GetAndStoreFingerprint(ByRef host As String) As String 
-        Try 
- 
-            'attempt a connect and load the fingerprint from initial connect. 
-            Dim startInfo As New ProcessStartInfo 
-            startInfo.FileName = Path.GetTempPath & "WinSCP.com" 
-            startInfo.RedirectStandardInput = True 
-            startInfo.RedirectStandardOutput = True 
-            startInfo.UseShellExecute = False 
-            startInfo.CreateNoWindow = True 
-            startInfo.WindowStyle = ProcessWindowStyle.Hidden 
- 
-            Dim process As New Process 
-            process.StartInfo = startInfo 
-            process.Start() 
-            process.StandardInput.WriteLine("open " & host) 
-            process.StandardInput.Close() 
-            process.WaitForExit() 
- 
-            'read the first line from the output of the command. 
-            Dim response As String = process.StandardOutput.ReadLine 
-            Dim rsa As String = "" 
- 
-            'loop through all lines of the output for the line containing 
-            'the servers fingerprint (hostkey) 
-            While response <> Nothing 
-                If response.Contains("ssh-rsa") Then 
-                    rsa = response 
-                    Exit While 
-                End If 
-                response = process.StandardOutput.ReadLine 
-            End While 
- 
-            'return the key 
-            Return rsa 
- 
-        Catch ex As Exception 
-        End Try 
- 
-        'return not found 
-        Return "" 
- 
-    End Function 
- 
-    Private Sub brnUPLOAD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brnUPLOAD.Click 
-        New_connection(tbx_HOST.Text, tbx_USER.Text, tbx_PW.Text, tbx_PORT.Text) 
-        If UploadFile(tbx_UPLOAD_remoteFilePath.Text, tbx_UPLOAD_localFilePath.Text) Then 
-            MsgBox("UPLOAD OK!", ) 
-        Else 
-            MsgBox("UPLOAD FAIL!", ) 
-        End If 
-    End Sub 
- 
-    Private Sub btnDOWNLOAD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDOWNLOAD.Click 
-        If System.IO.File.Exists(tbx_DOWNLOAD_localFilePath.Text) Then 
-            MsgBox("Waring: deleting file: " & tbx_DOWNLOAD_localFilePath.Text) 
-            File.Delete(tbx_DOWNLOAD_localFilePath.Text) 
-        End If 
-        If System.IO.File.Exists(tbx_DOWNLOAD_localFilePath.Text) Then 
-            MsgBox("Error: file not replaced!") 
-        Else 
-            New_connection(tbx_HOST.Text, tbx_USER.Text, tbx_PW.Text, tbx_PORT.Text) 
-            If DownloadFile(tbx_DOWNLOAD_remoteFilePath.Text, tbx_DOWNLOAD_localFilePath.Text) Then 
-                MsgBox("DOWNLOAD OK!", ) 
-            Else 
-                MsgBox("DOWNLOAD FAIL!", ) 
-            End If 
-        End If 
- 
-    End Sub 
- 
-    Private Sub WINSCP_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
- 
-    End Sub 
- 
-    Private Sub btnBROWSER1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBROWSER1.Click 
-        Dim Clicked As Integer = OpenFileDialog1.ShowDialog() 
-        If Clicked = Windows.Forms.DialogResult.Cancel Then 
-            MsgBox("Cancel Button Clicked") 
-        Else 
-            tbx_UPLOAD_localFilePath.Text = OpenFileDialog1.FileName 
-        End If 
-    End Sub 
- 
-    Private Sub btnBROWSER2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBROWSER2.Click 
-        Dim Clicked As Integer = SaveFileDialog1.ShowDialog() 
-        If Clicked = Windows.Forms.DialogResult.Cancel Then 
-            MsgBox("Cancel Button Clicked") 
-        Else 
-            tbx_DOWNLOAD_localFilePath.Text = SaveFileDialog1.FileName 
-            If System.IO.File.Exists(tbx_DOWNLOAD_localFilePath.Text) Then 
-                MsgBox("Waring: file exists!") 
-            End If 
-        End If 
-    End Sub 
- 
-    Private Sub btnEXIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEXIT.Click 
-        End 
-    End Sub 
- 
-    Private Sub btnLISTDIR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLISTDIR.Click 
-        New_connection(tbx_HOST.Text, tbx_USER.Text, tbx_PW.Text, tbx_PORT.Text) 
-        ListFile() 
-    End Sub 
-End Class 
- 
</code> </code>
<trailer> <trailer>
===== Further Reading ===== ===== Further Reading =====
- 
- 

Last modified: by martin