问题
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.
回答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
来源:https://stackoverflow.com/questions/28745899/load-save-documents-to-sftp-linux-server-with-vb-net