读取共享文件的内容

时间:2015-02-04 22:16:23

标签: vb.net visual-studio-2013

我想从另一台计算机上读取共享文件的内容(它有密码)。我搜索谷歌如何访问该文件,我自己编写代码:

 Option Explicit On
 Public Class Form1
Dim file As New Scripting.FileSystemObject
Dim readfile As Scripting.TextStream
Private Structure NETRESOURCE
    Public dwScope As UInteger
    Public dwType As UInteger
    Public dwDisplayType As UInteger
    Public dwUsage As UInteger
    Public lpLocalName As String
    Public lpRemoteName As String
    Public lpComment As String
    Public lpProvider As String
End Structure
Declare Function WNetAddConnection2 Lib "mpr.dll" Alias _
       "WNetAddConnection2A" (lpNetResource As NETRESOURCE, _
       ByVal lpPassword As String, ByVal lpUserName As String, _
       ByVal dwFlags As Long) As Long

Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _
"WNetCancelConnection2A" (ByVal lpName As String, _
ByVal dwFlags As Long, ByVal fForce As Long) As Long

Private Const NO_ERROR As UInteger = 0
Private Const RESOURCETYPE_DISK As UInteger = 1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    '' readfile = file.OpenTextFile(Process.Start("net.exe", "use \\Reznicencu-2025\share /USER:Reznicencu Reznicencu".ToString), Scripting.IOMode.ForReading)
    ''MsgBox(readfile.ReadAll)
    Test()
End Sub



Private Sub Test()
    Dim nr As New NETRESOURCE
    nr.dwType = RESOURCETYPE_DISK
    nr.lpRemoteName = "\\Reznicencu-2025\share"
    If WNetAddConnection2(nr, "Reznicencu", "Reznicencu", 0) <> NO_ERROR Then
        Throw New Exception("WNetAddConnection2 failed.")
    End If
    'Code to use connection here.'
    If WNetCancelConnection2("\\Reznicencu-2025\share", 0, True) <> NO_ERROR Then
        Throw New Exception("WNetCancelConnection2 failed.")
    End If
End Sub



End Class

我不确定它是否正确,因为它告诉我“'lpNetResource'不能通过类'Form1'在命名空间'Messenger'中暴露类型'NETRESOURCE'。”你能帮助我吗?

1 个答案:

答案 0 :(得分:1)

我意识到我改变了另一种形式的东西。这似乎是问题所在。