Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to get UNC-path

22 views
Skip to first unread message

Daniel Walzenbach

unread,
Feb 4, 2003, 7:25:25 PM2/4/03
to
Hi,

is there any posibility to get the UNC-path from a mapped directory?

Thanks for your help.

Daniel Walzenbach

Consultant

unread,
Feb 5, 2003, 11:01:08 AM2/5/03
to
do a net use from a cmd prompt.
but it is quite simple to figure out
\\server\share

"Daniel Walzenbach" <dani...@web.de> wrote in message
news:u7G9U0KzCHA.1768@TK2MSFTNGP12...

Daniel Walzenbach

unread,
Feb 5, 2003, 5:06:55 PM2/5/03
to
"Consultant" schrieb:

is there no way the .NET framework supports this feature?

Daniel Walzenbach

unread,
Feb 5, 2003, 5:14:30 PM2/5/03
to
"Consultant" schrieb:

I mean... it's nice to see the network mappings but how can I use them
in my program? could cou kindly post some lines of code?

Thanks a lot.

Vaibhav Modak

unread,
Feb 6, 2003, 6:38:24 AM2/6/03
to
Hi Daniel ,
You can use the "WScript.Network" class to get the UNC-path from a
mapped directory.
You have to use latebinding for the same.

Similarly you can use the same class to map Network directories.

Here is a Simple Class named "MapDirCode" which does both the tasks for you.

Public Class MapDirCode
Public Shared Sub main()
ViewMappedDrives()
MapNetworkDrives()
ViewMappedDrives()
End Sub

Shared Sub MapNetworkDrives()
Dim WshNetwork As Object
WshNetwork = CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive("G:", "\\Comp001\MyDir")
WshNetwork = Nothing
End Sub

Shared Sub ViewMappedDrives()
Dim WshNetwork As Object
Dim oDrives As Object
Dim i As Integer

WshNetwork = CreateObject("WScript.Network")
Console.WriteLine("Network Drive Mappings")
oDrives = WshNetwork.EnumNetworkDrives

For i = 0 To oDrives.Count - 1 Step 2
Console.WriteLine("Drive " & oDrives.Item(i) & " = " &
oDrives.Item(i + 1))
Next
oDrives = Nothing
WshNetwork = Nothing
End Sub
End Class

Regards
Vaibhav Modak
Maximizelearning
www.maximizelearning.com

"Daniel Walzenbach" <dani...@web.de> wrote in message
news:u7G9U0KzCHA.1768@TK2MSFTNGP12...

Lee Gillie

unread,
Feb 7, 2003, 1:52:18 PM2/7/03
to
I call WNetGetUniversalName directly from VB.NET rather than invoking
Scripting Runtime via COM.

HTH - Lee Gillie

"Daniel Walzenbach" <dani...@web.de> wrote in message
news:u7G9U0KzCHA.1768@TK2MSFTNGP12...

Vaibhav Modak

unread,
Feb 9, 2003, 11:31:58 PM2/9/03
to
Hi Lee,
I searched the MSDN for "WNetGetUniversalName" but I could not find the
VB.NET Code for the same.
The code that is given is only for VC/C++.

Can you elaborate the use of the same by giving a short Code Sample,

Thanks & Regards,
Vaibhav Modak
Maximizelearning
www.maximizelearning.com


"Lee Gillie" <DESP...@odp.comMAPSED> wrote in message
news:#u#e9ntzCHA.428@TK2MSFTNGP09...

Holger Boskugel

unread,
Feb 10, 2003, 5:56:58 AM2/10/03
to
Hello Vaibhav,

WNetGetUniversalName is Unicode function of the netapi32.dll ?! Declare one
of
the structs and place a declaration for this function in your code. With
lstrlenW and
lstrcpyW you get then the strings from the pointers.

Regards

Holger

"Vaibhav Modak" <vaib...@hotmail.com> schrieb im Newsbeitrag
news:OxUE8uL0CHA.1628@TK2MSFTNGP10...

Lee Gillie

unread,
Feb 10, 2003, 12:26:59 PM2/10/03
to
Vaibhav -

Sure...

You first need some declarations, which I do like this:

Private Declare Auto Function WNetGetUniversalName Lib "mpr.dll" ( _
<MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> _
ByVal lpLocalPath As String, _
ByVal dwInfoLevel As INFO_LEVEL, _
ByVal lpBuffer As IntPtr, _
ByRef lpBufferSize As Integer _
) As Integer

Private Structure REMOTE_NAME_INFO
<MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> _
Public lpUniversalName As String
<MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> _
Public lpConnectionName As String
<MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)> _
Public lpRemainingPath As String
End Structure

Private Enum INFO_LEVEL As Integer
UNIVERSAL_NAME_INFO_LEVEL = 1
REMOTE_NAME_INFO_LEVEL = 2
End Enum

And then, for my purposes, I create a VB wrapper function:

Public Function GetUniversalName( _
ByVal Path As String, _
ByRef UniversalName As String, _
ByRef ConnectionName As String, _
ByRef RemainingPath As String) As Boolean
' When successful, returns TRUE with UniversalName,
' ConnectionName, and RemainingPath data. If not
' successful, it may be the drive is local and not mapped.
Dim buffer As Integer
Dim ptrbuffer As IntPtr
Dim status As Integer
Dim rni As REMOTE_NAME_INFO
Dim Success As Boolean
Dim SafteyCount As Integer = 0

UniversalName = ""
ConnectionName = ""
RemainingPath = ""
buffer = 1024

ptrbuffer = Marshal.AllocHGlobal(buffer)
status = WNetGetUniversalName( _
Path, INFO_LEVEL.REMOTE_NAME_INFO_LEVEL, _
ptrbuffer, buffer)
Do While True
Select Case status
Case NO_ERROR
rni = Marshal.PtrToStructure(ptrbuffer, GetType(REMOTE_NAME_INFO))
UniversalName = rni.lpUniversalName
ConnectionName = rni.lpConnectionName
RemainingPath = rni.lpRemainingPath
Success = True
Exit Do
Case ERROR_MORE_DATA
If SafteyCount > 3 Then
Success = False
Exit Do
End If
SafteyCount += 1
Marshal.FreeHGlobal(ptrbuffer)
ptrbuffer = Marshal.AllocHGlobal(buffer)
status = WNetGetUniversalName(Path, _
INFO_LEVEL.REMOTE_NAME_INFO_LEVEL, ptrbuffer, buffer)
Case Else
Success = False
Exit Do
End Select
Loop
Marshal.FreeHGlobal(ptrbuffer)
Return Success
End Function

You could probably do this better, but the above seems to work for me.

Hope this helps.

- Lee Gillie

"Vaibhav Modak" <vaib...@hotmail.com> wrote in message
news:OxUE8uL0CHA.1628@TK2MSFTNGP10...

Vaibhav Modak

unread,
Feb 11, 2003, 3:40:13 AM2/11/03
to
Hi Lee,
Thanx for the code,
It was much "shorter" then what I expected . . . .
(Just Joking;-)

Regards,
Vaibhav Modak


"Lee Gillie" <DESP...@odp.comMAPSED> wrote in message

news:Or84QmS0CHA.1728@TK2MSFTNGP12...

0 new messages