browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need Visual Basic .NET help?

Get answers from our community of Visual Basic .NET experts on BYTES! It's free.

Find the UNC path of a file (.NET 2.0)

Pieter
Guest
 
Posts: n/a
#1: Nov 23 '05
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





Siva M
Guest
 
Posts: n/a
#2: Nov 23 '05

re: Find the UNC path of a file (.NET 2.0)


Hello,

Check
http://www.pinvoke.net/default.aspx/...ersalName.html.
However it only has the C# sample. But, you should be able to convert it to
Visual Basic .NET easily with conversion tools. One such tool is here:
http://www.developerfusion.co.uk/uti...btocsharp.aspx

Hope this helps.

"Pieter" <pietercoucke@hotmail.com> wrote in message
news:uZhWo4S6FHA.636@TK2MSFTNGP10.phx.gbl...
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



Christoph Wienands
Guest
 
Posts: n/a
#3: Nov 23 '05

re: Find the UNC path of a file (.NET 2.0)


Hey Pieter,

maybe the Uri class can do what you're looking for.

Christoph

"Pieter" <pietercoucke@hotmail.com> wrote in message
news:uZhWo4S6FHA.636@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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 ->[/color]
\\MyServer\MyShare\MyDirectory\MyFile.doc[color=blue]
> 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[/color]
NetShareEnum,[color=blue]
> but I couldn't find a working sample for this... Could anybody help me[/color]
with[color=blue]
> 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[/color]
'new'[color=blue]
> 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[/color]
UNIVERSAL_NAME_INFO,[color=blue]
> 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
>
>[/color]


Pieter
Guest
 
Posts: n/a
#4: Nov 23 '05

re: Find the UNC path of a file (.NET 2.0)


Hi,

I tryed and googled for it, but I wasn't able to find a solution? Could you
be a little bit more specific? It seems to me that this class is only for
URL's, not for local drives and mapped drives?

"Christoph Wienands" <cwienands@gmx.de> wrote in message
news:eFA3vhZ6FHA.3756@TK2MSFTNGP10.phx.gbl...[color=blue]
> Hey Pieter,
>
> maybe the Uri class can do what you're looking for.
>
> Christoph
>
> "Pieter" <pietercoucke@hotmail.com> wrote in message
> news:uZhWo4S6FHA.636@TK2MSFTNGP10.phx.gbl...[color=green]
>> 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 ->[/color]
> \\MyServer\MyShare\MyDirectory\MyFile.doc[color=green]
>> 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[/color]
> NetShareEnum,[color=green]
>> but I couldn't find a working sample for this... Could anybody help me[/color]
> with[color=green]
>> 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[/color]
> 'new'[color=green]
>> 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[/color]
> UNIVERSAL_NAME_INFO,[color=green]
>> 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
>>
>>[/color]
>
>[/color]


Pieter
Guest
 
Posts: n/a
#5: Nov 23 '05

re: Find the UNC path of a file (.NET 2.0)


Thanks, but that doesn't work with local files neither... :-/

"Siva M" <shiva_sm@online.excite.com> wrote in message
news:uOCLmaY6FHA.1000@tk2msftngp13.phx.gbl...[color=blue]
> Hello,
>
> Check
> http://www.pinvoke.net/default.aspx/...ersalName.html.
> However it only has the C# sample. But, you should be able to convert it
> to
> Visual Basic .NET easily with conversion tools. One such tool is here:
> http://www.developerfusion.co.uk/uti...btocsharp.aspx
>
> Hope this helps.
>
> "Pieter" <pietercoucke@hotmail.com> wrote in message
> news:uZhWo4S6FHA.636@TK2MSFTNGP10.phx.gbl...
> 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
>
>
>[/color]


Closed Thread