I neet to get UNC path from mapped drive. I tried to use WNetGetConnection, but it doesn't work for me. It returns error 487. Does anybody know how to deal with this error or any other way to get the UNC path?
Asked
Active
Viewed 1.4k times
3 Answers
4
Totally go with @Alex K's P/Invoke suggestion, I just wanted to post a hack method of piping through the net use
command:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim RemotePath = GetUncSourcePath("v"c)
If String.IsNullOrEmpty(RemotePath) Then
Trace.WriteLine("there was an error")
Else
Trace.WriteLine(RemotePath)
End If
Me.Close()
End Sub
Private Shared Function GetUncSourcePath(ByVal driveLetter As Char) As String
If String.IsNullOrEmpty(driveLetter) Then Throw New ArgumentNullException("driveLetter")
If (driveLetter < "a"c OrElse driveLetter > "z") AndAlso (driveLetter < "A"c OrElse driveLetter > "Z") Then Throw New ArgumentOutOfRangeException("driveLetter", "driveLetter must be a letter from A to Z")
Dim P As New Process()
With P.StartInfo
.FileName = "net"
.Arguments = String.Format("use {0}:", driveLetter)
.UseShellExecute = False
.RedirectStandardOutput = True
.CreateNoWindow = True
End With
P.Start()
Dim T = P.StandardOutput.ReadToEnd()
P.WaitForExit()
For Each Line In Split(T, vbNewLine)
If Line.StartsWith("Remote name") Then Return Line.Replace("Remote name", "").Trim()
Next
Return Nothing
End Function

Chris Haas
- 53,986
- 12
- 141
- 274
-
@rbsoft.sol, drop to a command line and run `net use v:`, replacing `v:` with your mapped drive. Do you see a line that says `Remote name`? – Chris Haas Sep 29 '11 at 13:06
-
@Chris..No, I get "The network connection could not be found"..I am using net use c: – Nitesh Sep 30 '11 at 04:14
-
@rbsoft.sol, unless you've got a really weird setup, your `c:` drive should be a local drive, not a network drive. This post is only about network drives. – Chris Haas Sep 30 '11 at 13:01
-
@Chris...You're correct, it is a local drive. However, I also tried with a network drive Z:, but got the same error. :( – Nitesh Oct 01 '11 at 01:25
-
This solution worked for me. I tested it on winxp, vista, win7, server 2003 and 2008. The thing to note is this only returns the unc path if the drive letter is mapped to a network drive. If it's a local drive like c: then it returns nothing. – goku_da_master Nov 18 '11 at 16:26
-
Why not use this solution instead of pinvoke? It's much less code which is always a good thing. – goku_da_master Nov 18 '11 at 16:30
-
1@goku_da_master, there's a couple of reasons why you might not want to use this. First, the fact that you had to test it to see what OS versions it did work on in the first place whereas the documentation for `WNetGetUniversalName` tells you straight away that it became supported with Win2K. Second, this method relies on string parsing which would break if the text isn't exactly the same, for instance if/when a new version came out or if there's a localized version of the `net` command. Third the API will give you more information such as `ERROR_CONNECTION_UNAVAIL` or `ERROR_NO_NETWORK`. – Chris Haas Nov 18 '11 at 17:15
-
@Chris, those are all valid points that I already suspected (except for your third point :)). And because of that, it seems riskier to use. But the fact it has functioned the same way (as far as my tests have shown) since winXp up until now, sounds like a pretty reliable candidate. If you add on the benefits of not having to use unmanaged code, and using less code (which makes it easier to maintain), makes it more appealing to me. – goku_da_master Nov 18 '11 at 17:50
-
1@goku_da_master, as the originator of the solution I would probably use it, I just wouldn't recommend it. If you've ever read Raymond Chen's blog you'll know that Microsoft has been cursed with fixing other people's bugs because they did things the way that appeared to work instead of the official documented way. Most of the shim system installed with Windows is a result of this. Also, this isn't really a managed solution because instead of P/Invoking you're still calling an unmanaged resource, `net.exe`. But if this works for you that's great, too! – Chris Haas Nov 18 '11 at 19:59
0
Works good for me and simpler than an api call like on http://vbnet.mvps.org/index.html?code/network/uncfrommappeddrive.htm The only thing is I had to add a line of code to Dim the variable Line.
Thanks for the help

ed v
- 1
-
Welcome to SO! This answer seems better suited as a comment, rather than an answer to the above question. – Boeckm Nov 06 '12 at 16:35