2015-02-04 118 views
1

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

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’不能在命名空间暴露型”NETRESOURCE“使者'通过班'Form1'“,你能帮我吗?

回答

1

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