I've been fumbling with WinSCP for a few days here now, and all I am missing is to "Open the Session" correctly.
Here the Session
is initialized.
Private Sub Loginbutton_Click(sender As Object, e As EventArgs) Handles Loginbutton.Click
mySession = New Session()
Dim mySessionOptions As New SessionOptions
With mySessionOptions
.Protocol = Protocol.Sftp
.HostName = "192.168.0.247"
.UserName = "usr"
.Password = "pw"
.SshHostKeyFingerprint = "ssh-rsa KEY"
End With
Using mySession As Session = New Session
mySession.Open(mySessionOptions)
End Using
Form1.Show()
Me.Close()
End Sub
On the following form I need to use the Session
again, and here I used the
Public Sub Open(ByVal sessionOptions As SessionOptions)
Me.Show()
For Each i As RemoteFileInfo In mySession.ListDirectory("/Database/").Files
Objectlist1.Items.Add(i.Name)
Next
Objectlist1.Refresh()
End Sub
This right here works. Which seems to open the session as required at this point. But my problem comes where I need to open the session multiple times in the same Form, as it does not Stay open by itself, if that's even possible.
Anything else I need to do in the application, is unable to open the Session as I cannot have several "Public Sub Open" down the line. If there is any WinSCP people here, can I make the session stay alive until application is closed? Any smart way of "Opening the Session" several times on the same form?
I find it hard to explain.
Example:
I have a ticker/timer, which refreshes the List located on Form 1 (Objectlist1)
And it ticks every 5.th second.
Here's the code used:
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
Objectlist1.Clear()
For Each i As RemoteFileInfo In mySession.ListDirectory("/Database").Files
Objectlist1.Items.Add(i.Name)
Next
End Sub
Problem is though, when it ticks, I get
Session is not opened, System.InvalidOperationException
which basically is.. Session is not opened.. Now, Since its opened further up in the form, I apparently need to open it again? Or can I change something in the
Public Sub Open(ByVal sessionOptions As SessionOptions)
to make it able to stay alive all through the form, or?
Any advise here guys?