VB.NET - WNetAddConnection2 - “找不到网络路径。”

时间:2014-01-09 16:42:50

标签: vb.net unc

我使用以下代码与网络路径建立连接以获取文件列表。

网络路径非常正确,我可以使用相同的凭据手动映射驱动器。运行此代码时,会显示错误:“53”,表示“找不到网络路径。”。

错误突然发生,这表明即使连接没有建立。

Dim nr As New NETRESOURCE
    nr.dwType = RESOURCETYPE_DISK
    nr.lpRemoteName = "\\fileserver.ourserver.com\"
    MessageBox.Show(WNetAddConnection2(nr, "Password", "ourserver.com\User", 0))

其他代码是:

Imports System.Runtime.InteropServices
Imports System
Imports System.IO


Public Class Form1


    <StructLayout(LayoutKind.Sequential)> _
    Private Structure NETRESOURCE
        Public dwScope As UInteger
        Public dwType As UInteger
        Public dwDisplayType As UInteger
        Public dwUsage As UInteger
        <MarshalAs(UnmanagedType.LPTStr)> _
        Public lpLocalName As String
        <MarshalAs(UnmanagedType.LPTStr)> _
        Public lpRemoteName As String
        <MarshalAs(UnmanagedType.LPTStr)> _
        Public lpComment As String
        <MarshalAs(UnmanagedType.LPTStr)> _
        Public lpProvider As String
    End Structure

    Private Const NO_ERROR As Long = 0
    Private Const RESOURCETYPE_DISK As UInteger = 1

    <DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function WNetAddConnection2(ByRef lpNetResource As NETRESOURCE, <[In](), MarshalAs(UnmanagedType.LPTStr)> ByVal lpPassword As String, <[In](), MarshalAs(UnmanagedType.LPTStr)> ByVal lpUserName As String, ByVal dwFlags As UInteger) As UInteger
    End Function

    <DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function WNetCancelConnection2(<[In](), MarshalAs(UnmanagedType.LPTStr)> ByVal lpName As String, ByVal dwFlags As UInteger, <MarshalAs(UnmanagedType.Bool)> ByVal fForce As Boolean) As UInteger
    End Function



End Class

2 个答案:

答案 0 :(得分:0)

您是否有使用互操作类列出文件的原因,您可以使用.net Framework到DiriectoryInfo类来执行相同操作。

答案 1 :(得分:0)

不确定原因,但是现在将/ IPC $添加到服务器路径可以让我再次使用它。