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

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
 
S

Siva M

Hello,

Check
http://www.pinvoke.net/default.aspx/advapi32/WNetGetUniversalName.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/utilities/convertvbtocsharp.aspx

Hope this helps.

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
 
C

Christoph Wienands

Hey Pieter,

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

Christoph
 
P

Pieter

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top