473,322 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
4 1660
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: hokieghal99 | last post by:
import os, string print " " setpath = raw_input("Enter the path: ") def find_replace(setpath): for root, dirs, files in os.walk(setpath): fname = files for fname in files: find =...
6
by: Peter Hansen | last post by:
Greetings. Im trying to write a program that can be run from the command line. If I want to search for example after a file with the ending .pdf, I should be able to write in the command line:...
1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
2
by: B Mahoney | last post by:
Is there a Python 'find' -like utility that will continue the file search through any zippped directory structure on the find path?
1
by: Alex | last post by:
Hello, in my company, we have a diagnostic tool for hardware. Depending on the hardware projcet, a different project configuration is needed. The configuration file is written in XML and...
0
by: haylow | last post by:
Hi I am new to ASP.NET and am working on an application that runs on a webserver. The user will open up the web interface in a browser on their local machine and enter a path to a directory. I...
0
by: sndive | last post by:
I have a weid problem. If i do this: import elementtree.ElementTree as ET .... tree = ET.parse("whatever") root = tree.getroot() r = root.find('last') print r return root where last is not an...
3
by: Alexander Vasilevsky | last post by:
Here, there is a challenge: to find the files must be on a local computer, have the same names. Get drives and folders to go through without problems. But what and how to store files while a...
8
by: inFocus | last post by:
Hello, I am new to python and wanted to write something for myself where after inputing two words it would search entire drive and when finding both names in files name would either copy or move...
0
by: Gabriel Genellina | last post by:
En Wed, 20 Aug 2008 17:10:15 -0300, aditya shukla <adityashukla1983@gmail.comescribió: You have 3 leading underscores in __main__ so the if statement is skipped and nothing happens. I'd use the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.