100分请教怎样在VB.NET中获得UNC路径,急!!!(顶者有分)
1、请问能否不用API获取UNC路径,如果可以请帮忙。
2、如果只能用API获取UNC路径,请问如何获得。
各位高手,拜托啦。
[解决办法]
to 2、如果只能用API获取UNC路径,请问如何获得。
Network Shares and UNC paths
http://www.codeproject.com/cs/internet/networkshares.asp
[解决办法]
http://www.vbgood.com/vb.good/article-do-view-articleid-2775.html
[解决办法]
http://www.songbai.com/vb/vbart/200606/vb_8473.html
这个上面有
[解决办法]
ding qi
[解决办法]
API的代码在.NET下也是能用的,很多API都还是用以前VB6老的方法
[解决办法]
不会,但帮忙顶
[解决办法]
帮你顶!
[解决办法]
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.
Usage
Dim strUNC As String
If GetUNCPath( "H: ", strUNC) = NO_ERROR Then
MsgBox "The UNC of the specified drive is " & strUNC
Else
MsgBox "There was a problem, sorry! "
End If
Code
注释: 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, _
lngRemoteName)
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)
Else
注释:Problems - so return original
注释:drive letter and error number
GetUNCPath = lngReturn
strUNCPath = strDriveLetter & " "
End If
GetUNCPath_End:
Exit Function
GetUNCPath_Err:
GetUNCPath = ERROR_NOT_SUPPORTED
strUNCPath = strDriveLetter
Resume GetUNCPath_End
End FunctionTip by Karl Moore