Click to See Complete Forum and Search --> : UNC drive/folder path
ultim8
November 21st, 2002, 09:35 AM
Anyone help on this one??
I am trying to retrieve the full UNC path for a network folder.
eg, if I have a file or folder G:\foldername, I want to return:
\\servername\foldername\
Have tried using the objects and methods within:
system.IO.directory
System.IO.DirectoryInfo
System.IO.FileInfo
everything I have tried just returns the drive letter.
Have unsuccessfully searched MSDN/Knowledgebase.
So can anyone out there help??
Please!!
Thanks
Steve.
ultim8
November 21st, 2002, 09:59 AM
By the way, this method does exist for projects:
FileSharePath Property
and
ActiveFileSharePath Property
but these are not available to directory, path or file objects....
Steve.
Athley
November 22nd, 2002, 02:14 AM
There is always the API possibility, I think this VB6 example should be rewritable:
Get UNC Path From a Mapped Network Share's Drive Letter (http://support.microsoft.com/default.aspx?scid=KB;en-us;q192689)
If I'll find another way, I'll get back.
/Leyan
ultim8
November 22nd, 2002, 07:02 AM
I will let you know if I can get this to work.
Thanks a lot.
Steve.
johnsoga
June 13th, 2003, 09:50 AM
Steve,
Did you ever figure out a way to get the UNC path in VB.Net or did you ever figure out how to convert the VB6 code found on the microsoft web site. If so could you give me the code to do that.
Thanks,
Gabe
DSJ
June 13th, 2003, 03:59 PM
Took a little tinkering, but here ya go:
Private Const RESOURCETYPE_ANY As Short = &H0s
Private Const RESOURCE_CONNECTED As Short = &H1s
Private Structure NETRESOURCE
Dim dwScope As Integer
Dim dwType As Integer
Dim dwDisplayType As Integer
Dim dwUsage As Integer
Dim lpLocalName As Integer
Dim lpRemoteName As Integer
Dim lpComment As Integer
Dim lpProvider As Integer
End Structure
Private Declare Function WNetOpenEnum Lib "mpr.dll" Alias "WNetOpenEnumA" (ByVal dwScope As Integer, ByVal dwType As Integer, ByVal dwUsage As Integer, ByRef lpNetResource As NETRESOURCE, ByRef lphEnum As Integer) As Integer
Private Declare Function WNetEnumResource Lib "mpr.dll" Alias "WNetEnumResourceA" (ByVal hEnum As Integer, ByRef lpcCount As Integer, ByRef lpBuffer As NETRESOURCE, ByRef lpBufferSize As Integer) As Integer
Private Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal hEnum As Integer) As Integer
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
MsgBox(LetterToUNC("H:"))
End Sub
Function LetterToUNC(ByRef DriveLetter As String) As String
Dim hEnum As Integer
Dim NetInfo(1023) As Form1.NETRESOURCE
Dim entries As Integer
Dim nStatus As Integer
Dim LocalName As String
Dim UNCName As String
Dim i As Integer
Dim r As Integer
' Begin the enumeration
nStatus = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_ANY, 0, NetInfo(0), hEnum)
LetterToUNC = "Drive Letter Not Found"
'Check for success from open enum
If ((nStatus = 0) And (hEnum <> 0)) Then
' Set number of entries
entries = 1024
' Enumerate the resource
nStatus = WNetEnumResource(hEnum, entries, NetInfo(0), CInt(Len(NetInfo(0))) * 1024)
' Check for success
If nStatus = 0 Then
For i = 0 To entries - 1
' Get the local name
LocalName = ""
If NetInfo(i).lpLocalName <> 0 Then
LocalName = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(New IntPtr(NetInfo(i).lpLocalName))
End If
If UCase(LocalName) = UCase(DriveLetter) Then
' Get the remote name
UNCName = ""
If NetInfo(i).lpRemoteName <> 0 Then
UNCName = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(New IntPtr(NetInfo(i).lpRemoteName))
End If
' Return the UNC path to drive
LetterToUNC = UNCName
' Exit the loop
Exit For
End If
Next i
End If
End If
' End enumeration
nStatus = WNetCloseEnum(hEnum)
End Function
johnsoga
June 16th, 2003, 09:49 AM
That is sweet. It works great. Thanks so much for you help this makes my job a lot easier.
Gabe
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.