所以我正在使用 Visual Studio 2013(社区)
到目前为止,我已经构建了一个可以使用文本框等创建文件的程序。
它保存到 XML,并希望从 XML 中读取(即使我的访问被拒绝)
现在是应用程序与服务器对话的时候了,所有文件都将保存并从中读取。
服务器是 Linux 服务器版(最新版),并且运行良好。 我希望我的应用程序连接到它、登录,然后只列出并从服务器读取文件。
到目前为止,它做了一点。
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
这就像一个魅力,它继续前进。
加载 Form1
后,它会显示服务器文件夹中的正确文件..
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
当我将文件保存到它时
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
所有这些都按预期工作。
我想提一下,问题中的文件夹,或服务器上有问题的“共享”,受密码保护,用户名和密码插入登录代码(临时)
当我双击文件(激活)以阅读它时,我的问题就出现了。
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."
这到底是怎么回事?我假设因为它可以列出该文件夹的内容,并且为了测试我给了每个人对该文件夹的完全访问权限(用户、组、其他)在 Linux 上的完全访问权限 (0777)
我这样说是为了测试它是否有帮助。
这可能超出了您的专业知识范围,因为它涉及库 WinSCP,实际上是一个 Linux 服务器。
因为它只是拒绝它的“读取 XML”功能,所以我一定非常接近?
我看到很多人建议其他第三方库,如果可能的话,最适合我的是纯 VB.NET 中的解决方案。
最佳答案
您正在将 SFTP 登录与通过 UNC 路径访问远程资源相结合。这行不通。要么只使用 SFTP(你可以使用 WinSCP .NET 程序集),要么登录到远程(Samba?)服务器,这样你就只能使用 UNC 路径。
接下来是 SFTP 解决方案。我不懂 VB.NET,所以请原谅语法错误。另请注意,您需要将 mySession
设为全局,以便您可以从其他函数访问它。
加载远程文件列表:
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
引用:https://winscp.net/eng/docs/library_session_listdirectory
保存:
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
引用:https://winscp.net/eng/docs/library_session_putfiles
正在加载:
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())
关于linux - 使用 VB.NET 将文档加载/保存到 SFTP Linux 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28745899/