Question Geting the networks path to files

wjburke2

Active member
Joined
Feb 3, 2009
Messages
29
Programming Experience
Beginner
I have a form with two listboxes. The user selected the directory using a FolderBrowserDialog1 then my program searches the directory and displays a list of files. They them select the files they want to store under a project name and the names are placed in a database. My problem is it stores the mapped file path not the network path. (ie N:\examples\testfile.txt I need \\myserver\examples\testfiles.txt) That is because the person that reads this database may not have their network drives mapped to the same letters.
 
There are several ways to do this, apart from enforcing a unified share mapping policy in the network, the easiest I've found is using the WNetGetConnection function.
VB.NET:
Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" ( _
        ByVal lpszLocalName As String, _
        ByVal lpszRemoteName As System.Text.StringBuilder, _
        ByRef cbRemoteName As Int32) As Int32
sample usage:
VB.NET:
Dim file As String = "z:\test.file"
If New IO.DriveInfo(file(0)).DriveType = IO.DriveType.Network Then
    Dim len As Integer = 260
    Dim unc As New System.Text.StringBuilder(len)
    If WNetGetConnection(file.Remove(2), unc, len) = 0 Then
        file = unc.ToString & file.Substring(2)
    End If
End If

'file string is now in my test: \\machinename\testshare\test.file
 
Back
Top