0

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?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
PiperMp3
  • 53
  • 9

1 Answers1

1

First, you are initializing a presumably global variable or class field mySession, as you got advised at Initialize Global Variable .Net:

    mySession = New Session()

But then you declare a local variable with the same name (which hides the global one for the rest of the scope):

    Using mySession As Session = New Session     

and you open the local one:

        mySession.Open(mySessionOptions)

and immediately close it:

    End Using

The global mySession is never opened.

You need to do:

    mySession = New Session()

    mySession.Open(mySessionOptions)
Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Ey Martin, Yea I thought it was something like that. I've done all you've advised so far, but the .Open(mySessionOptions) comes up on other forms as not Declared. See, In the first one, where I logged in remember, everything works fine. In the other forms, New Session works, but mySession.Open(mySessionOptions) is not declared for some reason. Am I missing something in a module? – PiperMp3 Mar 02 '15 at 12:20
  • Well, then the `mySession` has to be project-wide. See [Declare global variables in Visual Studio 2010 and VB.NET](http://stackoverflow.com/questions/4529862/declare-global-variables-in-visual-studio-2010-and-vb-net) – Martin Prikryl Mar 02 '15 at 12:22
  • Project-Wide! There is something that might work. Right, I'll check that out, and see how it goes ;D Thanks Prikryl! – PiperMp3 Mar 02 '15 at 12:25
  • Yeaah, that didn't work that well. Using Global Variables just renders "Object reference not set to an instance of an object." And before it was "Session not opened". Class is as it should be according to your instructions, and I have to use the Module ; Friend Module Module1 Public mySession As Session End Module, cos if Not the "Session" is rendered Undeclared. Idk Martin, it doesn't seem to work well together. Only reason the program is even starting, is cus of "Public Sub Open" which can only be used 1 time pr Winform. Either I am given wrong info, or something else is messed up. – PiperMp3 Mar 02 '15 at 12:48
  • I assume you didn't remove the form-specific `mySession`, so it hides the global one. – Martin Prikryl Mar 02 '15 at 12:49
  • I've used GlobalVariables.mySession to sort of guide it towards the Class. But I get the same errors on the same line of code. Isn't this what you mean? mySession.Open(GlobalVariables.mySessionOptions) And the GlobalVariables Class is; Public Class GlobalVariables Public Shared mySessionOptions As SessionOptions Public Shared mySession As Session End Class – PiperMp3 Mar 02 '15 at 12:52
  • On what line does it throw? Note that it makes no sense to move `SessionOptions` to global scope. It's just `Session` that is used globally. – Martin Prikryl Mar 02 '15 at 12:56
  • SessionOptions Removed. It throws here; Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick GlobalVariables.mySession = New Session() mySession.Open(GlobalVariables.mySessionOptions) mySession.Open(GlobarVariables.... – PiperMp3 Mar 02 '15 at 12:58
  • Wait, that was messed up.. It throws Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick GlobalVariables.mySession = New Session() [mySession.Open(GlobalVariables.mySessionOptions)] HERE – PiperMp3 Mar 02 '15 at 12:59
  • Did I provide wrong information? Need anything else? – PiperMp3 Mar 02 '15 at 14:14
  • 1) You still refer to global namespace in `mySession.Open(GlobalVariables.mySessionOptions)`. 2) You are not supposed to open the session in every method, just refer to the `mySession` variable. 3) Sorry, but this is getting nowhere. I suggest your starting learning programming on some easier task. – Martin Prikryl Mar 02 '15 at 14:21
  • Well, I am not the most experienced person here, but it seems to be that WinSCP works best in only one WinForm and not projectwide. I have followed your recipe on making it Projectwide but its only WinSCP errors I get. All examples on your website is one form only, and I would like to be able to do all my stuff in One form, but thats not what the application will be used for. And I have followed them to the last dott. WinSCP does work in only one form however, exactly as the example x) I appreciate the help provided so far, but I am rendered stuck regardless on NullReference :/ – PiperMp3 Mar 03 '15 at 12:13