VBGood网站全文搜索 Google

首页 - 经验之谈 - 获得通用命名标准路径
发表评论(0)作者:Karl Moore, 平台:VB6.0+Win98, 阅读:9155, 日期:2001-06-06
Get the UNC Path

Networked drives. They注释:re an administrative nightmare. In fact, the average user changes his networked drive letters more often than his underwear.

But you can solve this problem of binding an application to a particular drive by using a Universal Naming Convention (UNC) path(UNC(Universal Naming Conversion)通用命名标准。). This still references a network area, but doesn抰 tie it to any one drive letter.

And you can retrieve and check the UNC of a particular path in code using this neat little function.

To use it, simply call GetUNCPath, passing it your drive letter along with a pre-declared empty string. If a problem occurs, the relevant number is passed back with the function. These can be matched with the possible return code constants.

However if everything goes swimmingly, the function returns a zero (a constant value of NO_ERROR) and places the UNC path into the ByRef-passed variable.

This code works by making a call to the WNetGetConnection function in MPR.DLL. It注释:s essentially a neat wrapper for a regular API call.


    Dim strUNC As String
    If GetUNCPath("H:", strUNC) = NO_ERROR Then
        MsgBox "The UNC of the specified drive is " & strUNC
        MsgBox "There was a problem, sorry!"
    End If

注释: To be put inside a module

注释:Possible return codes from the API
Public Const ERROR_BAD_DEVICE           As Long = 1200
Public Const ERROR_CONNECTION_UNAVAIL   As Long = 1201
Public Const ERROR_EXTENDED_ERROR       As Long = 1208
Public Const ERROR_MORE_DATA            As Long = 234
Public Const ERROR_NOT_SUPPORTED        As Long = 50
Public Const ERROR_NO_NET_OR_BAD_PATH   As Long = 1203
Public Const ERROR_NO_NETWORK           As Long = 1222
Public Const ERROR_NOT_CONNECTED        As Long = 2250
Public Const NO_ERROR                   As Long = 0

注释:This API returns a UNC from a drive letter
Declare Function WNetGetConnection Lib "mpr.dll" Alias _
    "WNetGetConnectionA" _
    (ByVal lpszLocalName As String, _
    ByVal lpszRemoteName As String, _
    cbRemoteName As Long) As Long

Function GetUNCPath(ByVal strDriveLetter As String, _
                  ByRef strUNCPath As String) As Long

On Local Error GoTo GetUNCPath_Err

    Dim strMsg As String
    Dim lngReturn As Long
    Dim strLocalName As String
    Dim strRemoteName As String
    Dim lngRemoteName As Long

    strLocalName = strDriveLetter
    strRemoteName = String$(255, Chr$(32))
    lngRemoteName = Len(strRemoteName)

    注释:Attempt to grab UNC
    lngReturn = WNetGetConnection(strLocalName, _
                                  strRemoteName, _

    If lngReturn = NO_ERROR Then
        注释:No problems - return the UNC
        注释:to the passed ByRef string
        GetUNCPath = NO_ERROR
        strUNCPath = Trim$(strRemoteName)
        strUNCPath = Left$(strUNCPath, Len(strUNCPath) - 1)
        注释:Problems - so return original
        注释:drive letter and error number
        GetUNCPath = lngReturn
        strUNCPath = strDriveLetter & "\"
    End If
    Exit Function
    strUNCPath = strDriveLetter
    Resume GetUNCPath_End
End FunctionTip by Karl Moore