0

所以我正在使用 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 的解决方案。

4

1 回答 1

1

您将 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()) 

参考:https ://winscp.net/eng/docs/library_session_getfiles

于 2015-02-27T10:15:37.873 回答