UNC to regular path (not the other way!)
Umm i had another post like this, seems like people still didnt understand what I want:D
I want to convert a UNC path back to a windows path with drive letters in it. How would i do it?
ie, i want to convert \\server\share to "d:" if that share is mapped to drive D on the local computer
Re: UNC to regular path (not the other way!)
MrPolite, that was exactly what the example I posted for you did :(
what about it didn't do what you were saying? you type in a UNC path in the textbox, and the code will look at all your drive letters to see if one matches that UNC, and gives you the drive letter of the match, if present...
Re: UNC to regular path (not the other way!)
Have you tried FileSystemObject.
Probabaly, this function is what you are looking for.
VB Code:
Private Function convertUNCToDrive(ByVal pathName As String) As String
Dim fileSystem As New FileSystemObjectClass
Dim pathToReturn As String
'loop through all drives
For Each drv As Drive In fileSystem.Drives
'check if the drive is mapped
If drv.DriveType = DriveTypeConst.Remote Then
'check if the share is same as the path mentioned
If pathName.Substring(0, drv.ShareName.Length) = drv.ShareName.ToString Then
'replace it with drive letter
pathToReturn = pathName.Replace(drv.ShareName, drv.Path)
Exit For
End If
End If
Next
Return pathToReturn
End Function
Add a reference to Microsoft Scripting Runtime. And put Imports Scripting at the top.
Re: UNC to regular path (not the other way!)
The wmi sample I had mentioned would do it as well, you would loop all instances of the Win32_LogicalDisk WMI class, see if the "DriveType" property is "4" (meaning Network Drive), then see if the "ProviderName" property (which is the property that lists the network drive UNC path) equals the UNC path you have, then the drive letter is the "name" property of the same object...
I could do a sample that does this if you really need it, but it seems that you have several examples that might work as well...
Re: UNC to regular path (not the other way!)
Quote:
Originally Posted by kleinma
MrPolite, that was exactly what the example I posted for you did :(
what about it didn't do what you were saying? you type in a UNC path in the textbox, and the code will look at all your drive letters to see if one matches that UNC, and gives you the drive letter of the match, if present...
oooooooh sorry
i dont have visual studio.net.... i was just looking at the API call and judging by what that API call does i thought it's doign the opposite of what i wanted:D
okay it's a start... now i guess i should ask the rest in a different forum. Thanks for the solutions guys. I;m doing this in C++ so i should use APIs only not .NET....
:afrog:
Re: UNC to regular path (not the other way!)
you may struggle the api way, you can sometimes get the GetLongPathName api to work, but it can raise issues over server privilages.