- Joined
- Apr 21, 2011
- Messages
- 1
- Reaction score
- 0
I've recently created a function that will do this, without the need of adding additional references to the project.
Imports Microsoft.Win32
Public Function getDriveLetter(ByVal uncPath As String) As String
Dim myLetter As String = ""
Dim i As String = ""
Dim myKey AsRegistryKey = Registry.CurrentUser
Dim keyCheck AsRegistryKey
myKey = myKey.OpenSubKey("Network")
Dim myKeys() As String = myKey.GetSubKeyNames
For Each i In myKeys
keyCheck = myKey.OpenSubKey(i)
If keyCheck.GetValue("RemotePath").ToString.ToUpper = uncPath.ToUpper Then
myLetter = i & ":\"
End If
Next
If myLetter = ""Then
myLetter = uncPath & "\"
End If
Return myLetter
End Function
What this does is simply look at all existing network drives in the registry, compares the UNC paths, and returns the drive that corresponds to it.
If you had an F: drive that corresponded to \\server\Production, you could pass \\server\production, and it would return "F:\", or \\server\production\ if you didn't have a mapped drive to it.
I was pulling my hair out until I figured out how to do this, so hopefully this helps somebody else. Laters!
-Sir Phoenix
Imports Microsoft.Win32
Public Function getDriveLetter(ByVal uncPath As String) As String
Dim myLetter As String = ""
Dim i As String = ""
Dim myKey AsRegistryKey = Registry.CurrentUser
Dim keyCheck AsRegistryKey
myKey = myKey.OpenSubKey("Network")
Dim myKeys() As String = myKey.GetSubKeyNames
For Each i In myKeys
keyCheck = myKey.OpenSubKey(i)
If keyCheck.GetValue("RemotePath").ToString.ToUpper = uncPath.ToUpper Then
myLetter = i & ":\"
End If
Next
If myLetter = ""Then
myLetter = uncPath & "\"
End If
Return myLetter
End Function
What this does is simply look at all existing network drives in the registry, compares the UNC paths, and returns the drive that corresponds to it.
If you had an F: drive that corresponded to \\server\Production, you could pass \\server\production, and it would return "F:\", or \\server\production\ if you didn't have a mapped drive to it.
I was pulling my hair out until I figured out how to do this, so hopefully this helps somebody else. Laters!
-Sir Phoenix
Last edited: