P
Pieter
Hi,
I'm trying to get the whole UNC path of a file, and I would prefer to have
it via a .NET-way (I'm using VB.NET 2005).
So what I am basicly looking for is:
X:\MyDirectory\MyFile.doc -> \\MyServer\MyShare\MyDirectory\MyFile.doc
C:\MyDoc.doc -> \\MyComputer\C$\MyDoc.doc
I found a way to have it for a mapped drive via WNetGetUniversalName, and
normally I should be able to find it for a local harddisk via NetShareEnum,
but I couldn't find a working sample for this... Could anybody help me with
this? Where I can find a working VB.NET sample?
And to be honnest: I don't really like these solutions: Isn't there a 'new'
way to accomplish this? Those are really basic things in my opinion, and I
can't imagine that there isn't an easier way in the 2.0 Framework?
Thanks a lot in advance,
Pieter
My 'solution' for a mapped drive...
<DllImport("mpr.dll")> _
Private Shared Function WNetGetUniversalName(ByVal lpLocalPath As
String, ByVal dwInfoLevel As Integer, ByRef lpBuffer As UNIVERSAL_NAME_INFO,
ByRef lpBufferSize As Integer) As Integer
End Function
<DllImport("mpr", CharSet:=CharSet.Auto)> _
Protected Shared Function WNetGetUniversalName(ByVal lpLocalPath As
String, ByVal dwInfoLevel As Integer, ByVal lpBuffer As IntPtr, ByRef
lpBufferSize As Integer) As Integer
End Function
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure UNIVERSAL_NAME_INFO
<MarshalAs(UnmanagedType.LPTStr)> _
Public lpUniversalName As String
End Structure
Protected Const NO_ERROR As Integer = 0
Protected Const ERROR_MORE_DATA As Integer = 234
Protected Const ERROR_NOT_CONNECTED As Integer = 2250
Protected Const UNIVERSAL_NAME_INFO_LEVEL As Integer = 1
Public Function GetUNCPath(ByVal mappedDrive As String) As String
Dim rni As UNIVERSAL_NAME_INFO = New UNIVERSAL_NAME_INFO
Dim bufferSize As Integer = Marshal.SizeOf(rni)
Dim nRet As Integer = WNetGetUniversalName(mappedDrive,
UNIVERSAL_NAME_INFO_LEVEL, rni, bufferSize)
If ERROR_MORE_DATA = nRet Then
Dim pBuffer As IntPtr = Marshal.AllocHGlobal(bufferSize)
Try
nRet = WNetGetUniversalName(mappedDrive,
UNIVERSAL_NAME_INFO_LEVEL, pBuffer, bufferSize)
If NO_ERROR = nRet Then
rni = CType(Marshal.PtrToStructure(pBuffer,
GetType(UNIVERSAL_NAME_INFO)), UNIVERSAL_NAME_INFO)
End If
Finally
Marshal.FreeHGlobal(pBuffer)
End Try
End If
Select Case nRet
Case NO_ERROR
Return rni.lpUniversalName
Case ERROR_NOT_CONNECTED
MessageBox.Show("Share not connected")
Return String.Empty
Case Else
Return String.Empty
End Select
Return String.Empty
End Function
I'm trying to get the whole UNC path of a file, and I would prefer to have
it via a .NET-way (I'm using VB.NET 2005).
So what I am basicly looking for is:
X:\MyDirectory\MyFile.doc -> \\MyServer\MyShare\MyDirectory\MyFile.doc
C:\MyDoc.doc -> \\MyComputer\C$\MyDoc.doc
I found a way to have it for a mapped drive via WNetGetUniversalName, and
normally I should be able to find it for a local harddisk via NetShareEnum,
but I couldn't find a working sample for this... Could anybody help me with
this? Where I can find a working VB.NET sample?
And to be honnest: I don't really like these solutions: Isn't there a 'new'
way to accomplish this? Those are really basic things in my opinion, and I
can't imagine that there isn't an easier way in the 2.0 Framework?
Thanks a lot in advance,
Pieter
My 'solution' for a mapped drive...
<DllImport("mpr.dll")> _
Private Shared Function WNetGetUniversalName(ByVal lpLocalPath As
String, ByVal dwInfoLevel As Integer, ByRef lpBuffer As UNIVERSAL_NAME_INFO,
ByRef lpBufferSize As Integer) As Integer
End Function
<DllImport("mpr", CharSet:=CharSet.Auto)> _
Protected Shared Function WNetGetUniversalName(ByVal lpLocalPath As
String, ByVal dwInfoLevel As Integer, ByVal lpBuffer As IntPtr, ByRef
lpBufferSize As Integer) As Integer
End Function
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Private Structure UNIVERSAL_NAME_INFO
<MarshalAs(UnmanagedType.LPTStr)> _
Public lpUniversalName As String
End Structure
Protected Const NO_ERROR As Integer = 0
Protected Const ERROR_MORE_DATA As Integer = 234
Protected Const ERROR_NOT_CONNECTED As Integer = 2250
Protected Const UNIVERSAL_NAME_INFO_LEVEL As Integer = 1
Public Function GetUNCPath(ByVal mappedDrive As String) As String
Dim rni As UNIVERSAL_NAME_INFO = New UNIVERSAL_NAME_INFO
Dim bufferSize As Integer = Marshal.SizeOf(rni)
Dim nRet As Integer = WNetGetUniversalName(mappedDrive,
UNIVERSAL_NAME_INFO_LEVEL, rni, bufferSize)
If ERROR_MORE_DATA = nRet Then
Dim pBuffer As IntPtr = Marshal.AllocHGlobal(bufferSize)
Try
nRet = WNetGetUniversalName(mappedDrive,
UNIVERSAL_NAME_INFO_LEVEL, pBuffer, bufferSize)
If NO_ERROR = nRet Then
rni = CType(Marshal.PtrToStructure(pBuffer,
GetType(UNIVERSAL_NAME_INFO)), UNIVERSAL_NAME_INFO)
End If
Finally
Marshal.FreeHGlobal(pBuffer)
End Try
End If
Select Case nRet
Case NO_ERROR
Return rni.lpUniversalName
Case ERROR_NOT_CONNECTED
MessageBox.Show("Share not connected")
Return String.Empty
Case Else
Return String.Empty
End Select
Return String.Empty
End Function