Load/save 文件到 SFTP Linux 服务器 VB.NET
Load/save documents to SFTP Linux server with VB.NET
所以我正在使用 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
所有这些,都按预期工作。
我想提一下,问题中的文件夹,或服务器上有问题的 "Share",受密码保护,用户名和密码插入登录代码(临时)
当我双击(激活)文件以阅读它时,我的问题就出现了。
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 服务器。
因为只有"Read 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())
所以我正在使用 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
所有这些,都按预期工作。
我想提一下,问题中的文件夹,或服务器上有问题的 "Share",受密码保护,用户名和密码插入登录代码(临时)
当我双击(激活)文件以阅读它时,我的问题就出现了。
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 服务器。
因为只有"Read 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())