我正在编写一个文档索引系统,它将通过具有许多计算机驱动器的计算机网络引用 100 个文件。当添加新驱动器或从网络中删除驱动器时,将重新分配驱动器号。因此,如果重新分配驱动器号,则使用驱动器号的任何特定文件路径都可能变得毫无意义。为了避免这个问题,我想改用 UNC(通用命名约定)路径名。
下面显示的代码将在 Excel 2007 中运行时将驱动器号转换为其 UNC 等效项。但是我的应用程序是在 Visual Studio 2010 中编写的,不幸的是,下面的代码似乎在 Visual Studio 中不起作用。运行时不报告错误,但不返回 UNC。这可能是需要设置的“参考”的问题。
Imports System
Imports System.Management
Module Module1
' 32-bit Function version.
' Enter this declaration on a single line.
Declare Function WNetGetConnection Lib "MPR.DLL" Alias _
"WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal _
lpszRemoteName As String, ByVal lSize As Long) As Long
Dim lpszRemoteName As String
Dim lSize As Long
' Use for the return value of WNetGetConnection() API.
Const NO_ERROR As Long = 0
' The size used for the string buffer. Adjust this if you
' need a larger buffer.
Const lBUFFER_SIZE As Long = 255
Sub GetNetPath()
Dim DriveLetter, lpszLocalName As String
Dim cbRemoteName As Long
Dim lStatus&
' Prompt the user to type the mapped drive letter.
DriveLetter = UCase(InputBox("Enter Drive Letter of Your Network" & _
"Connection." & Chr(10) & "i.e. F (do not enter a colon)"))
' Add a colon to the drive letter entered.
lpszLocalName = DriveLetter & ":"
' Specifies the size in characters of the buffer.
' Prepare a string variable by padding spaces.
lpszRemoteName = lpszRemoteName & Space(lBUFFER_SIZE)
cbRemoteName = Len(lpszRemoteName)
' Return the UNC path (\\Server\Share).
lStatus& = WNetGetConnection(lpszLocalName, lpszRemoteName, _
cbRemoteName)
' Verify that the WNetGetConnection() succeeded. WNetGetConnection()
' returns 0 (NO_ERROR) if it successfully retrieves the UNC path.
If lStatus& = NO_ERROR Then
' Display the UNC path.
MsgBox(Left$(lpszRemoteName, cbRemoteName))
Else
' Unable to obtain the UNC path.
MsgBox("Unable to obtain the UNC path.")
End If
End Sub
End Module
任何建议都非常感激。非常感谢