By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,918 Members | 2,040 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,918 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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" <pi**********@hotmail.com> wrote in message
news:uZ*************@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

Nov 22 '05 #2

P: n/a
Hey Pieter,

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

Christoph

"Pieter" <pi**********@hotmail.com> wrote in message
news:uZ*************@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

Nov 22 '05 #3

P: n/a
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" <cw*******@gmx.de> wrote in message
news:eF**************@TK2MSFTNGP10.phx.gbl...
Hey Pieter,

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

Christoph

"Pieter" <pi**********@hotmail.com> wrote in message
news:uZ*************@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


Nov 22 '05 #4

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

"Siva M" <sh******@online.excite.com> wrote in message
news:uO**************@tk2msftngp13.phx.gbl...
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" <pi**********@hotmail.com> wrote in message
news:uZ*************@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

Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.