【问题标题】:VB.NET - WNetAddConnection2 - "The network path was not found."VB.NET - WNetAddConnection2 - “找不到网络路径。”
【发布时间】:2014-01-10 20:54:34
【问题描述】:

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

网络路径绝对正确,我可以使用相同的凭据手动映射驱动器。刚运行这段代码时,显示错误:“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

【问题讨论】:

  • StructLayout 属性需要 CharSet:=CharSet.Auto 以使其与函数声明匹配。

标签: vb.net unc


【解决方案1】:

您使用互操作类列出文件是否有原因,您可以使用 .net 框架到 DiriectoryInfo 类来做同样的事情。

【讨论】:

  • 旧代码,以前使用不同的方法,但现在使用 DirectoryInfo。
【解决方案2】:

不知道为什么,但是现在将 /IPC$ 添加到服务器路径可以让我再次使用它。

【讨论】:

    猜你喜欢
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 2015-03-04
    • 2017-07-04
    相关资源
    最近更新 更多