08. september 2005 - 14:13
Der er
1 kommentar og
1 løsning
UNC fra FolderBrowserDialog
Jeg har en aplikation hvor jeg skal kunne vælge en udbox til fejl meddelelser. Da stien skal anvendes af en windows service er jeg nød til at få stien som UNC.
Det er muligvis trivielt, men så har jeg sovet i timen. Er der nogen med et hurtigt svar?
09. september 2005 - 12:48
#1
JEg har selv fundet svaret, så der er ingen grund til at holde tråden åben.
Jeg smider lige koden hvis der er andre der skulle være interesseret.
Erklæringer****
Private Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" _
(ByVal lpszLocalName As String, ByVal lpszRemoteName As String, ByRef cbRemoteName As Integer) As Integer
' returns errors for UNC Path
Private Const ERROR_BAD_DEVICE = 1200&
Private Const ERROR_CONNECTION_UNAVAIL = 1201&
Private Const ERROR_EXTENDED_ERROR = 1208&
Private Const ERROR_MORE_DATA = 234
Private Const ERROR_NOT_SUPPORTED = 50&
Private Const ERROR_NO_NET_OR_BAD_PATH = 1203&
Private Const ERROR_NO_NETWORK = 1222&
Private Const ERROR_NOT_CONNECTED = 2250&
Private Const NO_ERROR = 0
Private Const ERROR_FILENOTFOUND = 53
Kode********************************
Public Function uncFromPath(ByVal path As String) As String
'pre: Sti til mappe
'post: UNC til samme mappe
On Error GoTo fGetUNCPath_Err
Dim Msg As String = ""
Dim lngReturn As Long
Dim lpszLocalName As String
Dim lpszRemoteName As String
Dim cbRemoteName As Long
path = Strings.Left(path, 1) & ":"
lpszLocalName = path
lpszRemoteName = Strings.StrDup(255, Strings.Chr(32))
cbRemoteName = Len(lpszRemoteName)
lngReturn = WNetGetConnection(lpszLocalName, lpszRemoteName, cbRemoteName)
If lpszRemoteName.Trim = "" Then
lngReturn = 0
End If
Select Case lngReturn
Case ERROR_BAD_DEVICE
Msg = "Error: Bad Device"
Case ERROR_CONNECTION_UNAVAIL
Msg = "Error: Connection Un-Available"
Case ERROR_EXTENDED_ERROR
Msg = "Error: Extended Error"
Case ERROR_MORE_DATA
Msg = "Error: More Data"
Case ERROR_NOT_SUPPORTED
Msg = "Error: Feature not Supported"
Case ERROR_NO_NET_OR_BAD_PATH
Msg = "Error: No Network Available or Bad Path"
Case ERROR_NO_NETWORK
Msg = "Error: No Network Available"
Case ERROR_NOT_CONNECTED
Msg = "Error: Not Connected"
Case NO_ERROR
' Det gik godt
Msg = ""
Case Else
Msg = "Unknown error: " & lngReturn.ToString
End Select
If Msg.Trim <> "" Then
MsgBox(Msg, vbInformation)
Else
uncFromPath = Left$(lpszRemoteName, cbRemoteName)
If uncFromPath.Trim.Length > 0 Then
uncFromPath = uncFromPath.Substring(2, Strings.InStr(3, uncFromPath, "\") - 3)
End If
End If
fGetUNCPath_End:
Exit Function
fGetUNCPath_Err:
MsgBox(Err.Description, vbInformation)
Resume fGetUNCPath_End
End Function