0

So I am using Visual Studio 2013 (Community)

And so far I've build a program that can create files using textboxes and so forth.

It saves to XML, and hopefully reads from XML (Even if I am getting access denied)

The time has come for the Application to talk to a server, where all the files will be saved, and read from.

The server is a Linux Server Edition (Latest) and its up and running fine. I want my application to connect to it, log in, and then just list and read files from the server.

So far, it does this a bit.

Private Sub Loginbutton_Click(sender As Object, e As EventArgs) Handles Loginbutton.Click

    Dim mySessionOptions As New SessionOptions
    With mySessionOptions
        .Protocol = Protocol.Sftp
        .HostName = "192.168.0.247"
        .UserName = "username" - these are default on purpose
        .Password = "password"
        .SshHostKeyFingerprint = "ssh-rsa 2048 [Hidden]"
    End With
    Using mySession As Session = New Session
        ' Connect
        mySession.Open(mySessionOptions)
    End Using
    Form1.Show()
    Me.Close()

End Sub

That works like a charm, and it moves on.

Once Form1 is loaded, its showing me the correct files from the server folder..

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    For Each i As String In Directory.GetFiles("\\192.168.0.247\Database")
        Objectlist1.Items.Add(Path.GetFileName(i))
    Next
    Objectlist1.Refresh()

End Sub

And when I save files to it

Private Sub Savebutton_Click(sender As Object, e As EventArgs) Handles Savebutton.Click

    If IO.File.Exists(Pholderbox.Text) = False Then

        Dim settings As New XmlWriterSettings()
        settings.Indent = True

        Dim XmlWrt As XmlWriter = XmlWriter.Create("\\192.168.0.247\Database\" + Pholderbox.Text, settings)
        With XmlWrt

All of that, works as intended.

I want to mention that the folder in Question, or "Share" in question on the server, is password protected, and the username and password are inserted in the Login Code (Temporary)

My problem comes here when I doubleclick the file (activate) to READ it.

Private Sub Objectlist1_ItemActivate(sender As Object, e As EventArgs) Handles Objectlist1.ItemActivate
    Caseworker.Show()

    Me.Objectlist1.MultiSelect = False

    Dim selectedListViewItem As String
    selectedListViewItem = Me.Objectlist1.SelectedItems.Item(0).ToString
    Const basepath As String = "\\192.168.0.247\Database"
    Dim xmlpath = IO.Path.Combine(basepath, Objectlist1.SelectedItems.Item(0).Text)
    If (IO.File.Exists(xmlpath)) Then

        Dim document As XmlReader = New XmlTextReader(basepath)

        Dim mySessionOptions As New SessionOptions

        While (document.Read())
        ' - This little bugger screams out everytime
        ' "An unhandled exception of type 'System.UnauthorizedAccessException' occurred in System.Xml.dll
        ' Additional information: Access to the path '\\192.168.0.247\Database' is denied." 

What in the world is wrong here? I would assume since it can list the content of that folder, and for testing I gave EVERYONE Full access to that folder (User, Group, Other) FULL Access on Linux (0777)

I put it like that to test if it would help.

This might be out of your expertize, as it involves the library WinSCP and is in fact a Linux Server.

Being that its only the "Read XML" feature that denies it, I must be very close?

I see that very many suggest other Third Party libraries, the best for me, would be a solution in plain VB.NET if possible.

Martin Prikryl
  • 147,050
  • 42
  • 335
  • 704
PiperMp3
  • 53
  • 8
  • Oh right. If anyone wonders,or if it helps, these are my Imports; Imports System.IO Imports System.Xml Imports System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock Imports System.Windows.Forms Imports System.Xml.XmlReader Imports System.Windows.Forms.TextBox Imports System Imports System.Windows.Forms.ListView Imports System.Net Imports WinSCP – PiperMp3 Feb 26 '15 at 15:08
  • If you shutdown your application, restart it, and try to open the file, without saving it first, does it work? – Martin Prikryl Feb 26 '15 at 15:47
  • Stop Debugging, Start Debugging, Open the file that was saved/Created earlier on, you mean? I can't test until tomorrow, as the office closed. But I will give it a go and get back to you! – PiperMp3 Feb 26 '15 at 15:53
  • Yes, that's what I mean. – Martin Prikryl Feb 26 '15 at 15:54
  • Right, tried that. No difference. But I found out something new. This /Database folder, is a Shared Folder on the Linux Machine, and it can be accessed by using a shortcut on the desktop. Now I am NOT Logged in via the shortcut, now I am getting "Access Denied" when the program is just supposed to list the files (without creating or reading) and its like the VB application is not using the credentials provided in the code at all. Wat – PiperMp3 Feb 27 '15 at 09:33
  • Well it definitely does not use the credentials. Your code makes no sense. Logging in via SFTP, does not have any impact on you later using UNC path to access the file. But that does not explain why you can write and not read. – Martin Prikryl Feb 27 '15 at 09:42
  • I'm sorry, I used some third party library with WinSCP, and this was their example code for starting a session. I modified it to suit my server, but clearly it does not work. – PiperMp3 Feb 27 '15 at 09:46
  • If you can provide any help outside of WinSCP and going VB.net Codes only, I would prefer that. – PiperMp3 Feb 27 '15 at 09:46
  • The WinSCP code works. But you cannot combine logging in with SFTP and then using UNC paths. Either work over SFTP only (that's what you can use WinSCP .NET assembly for) or login to the UNC path and access the files via UNC path. But you cannot combine SFTP login with UNC read/writes. You can download/uploads files using WinSCP by calling [`Session.GetFiles`](http://winscp.net/eng/docs/library_session_getfiles) or [`Session.PutFiles`](http://winscp.net/eng/docs/library_session_putfiles). – Martin Prikryl Feb 27 '15 at 09:50
  • So technically, WinSCP Assembly will work with the current XML Reader, and XML Writer? I apologise for the questions, this all just hurts my head a little. I will get on it, and research more about WinSCP, and see how I can impliment it seamlessly to create and read files back and forth. – PiperMp3 Feb 27 '15 at 09:55

1 Answers1

1

You are combining SFTP login with an access to a remote resource via UNC path. This cannot work. Either use the SFTP only (what you can use WinSCP .NET assembly for) or login to the remote (Samba?) server, so that you can use UNC paths only.

An SFTP solution follows. I do not know VB.NET, so excuse mistakes in syntax. Also note, that you need to make the mySession global, so that you can access it from other functions.

Loading list of remote files:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    For Each i As RemoteFileInfo In mySession.ListDirectory("/Database").Files
        Objectlist1.Items.Add(i.Name)
    Next
    Objectlist1.Refresh()

End Sub

Reference: https://winscp.net/eng/docs/library_session_listdirectory

Saving:

Private Sub Savebutton_Click(sender As Object, e As EventArgs) Handles Savebutton.Click

    Dim settings As New XmlWriterSettings()

    settings.Indent = True

    Dim TempPath As String = IO.Path.Combine(IO.Path.GetTempPath, Pholderbox.Text);
    Dim XmlWrt As XmlWriter = XmlWriter.Create(TempPath , settings)
    With XmlWrt
    End With

    mySession.PutFiles(TempPath, "/Database/").Check()

End Sub

Reference: https://winscp.net/eng/docs/library_session_putfiles

Loading:

Private Sub Objectlist1_ItemActivate(sender As Object, e As EventArgs) Handles Objectlist1.ItemActivate Caseworker.Show()

    Me.Objectlist1.MultiSelect = False

    Dim selectedListViewItem As String
    selectedListViewItem = Me.Objectlist1.SelectedItems.Item(0).ToString

    Dim xmlpath = IO.Path.Combine(IO.Path.GetTempPath, Objectlist1.SelectedItems.Item(0).Text)

    mySession.GetFiles("/Database/" + Objectlist1.SelectedItems.Item(0).Text, xmlpath).Check();

    If (IO.File.Exists(xmlpath)) Then

        Dim document As XmlReader = New XmlTextReader(basepath)

        Dim mySessionOptions As New SessionOptions

        While (document.Read()) 

Reference: https://winscp.net/eng/docs/library_session_getfiles

Martin Prikryl
  • 147,050
  • 42
  • 335
  • 704
  • 1
    Aaaah, I see what you did there. You have given me help to continue, and I am thankful for that. Consider this case closed. Thanks a million Martin. – PiperMp3 Feb 27 '15 at 10:20