Solved

Get UNC path of a network drive

Posted on 2004-04-21
5
1,085 Views
Last Modified: 2007-12-19
How do I get the unc path of a drive letter in VB.Net?


Here is what my function definition should look like:
Public Shared Function fnGetUncPath(ByVal strDriveLetterAs String) As String


This is an example of how the function should work:
?fnGetUncPath("U")
\\SERVER\USERNAME

I have tried unsuccessfully to implement the WNetGetUniversalName api.

Thanks in advance
0
Comment
Question by:eeyore7250
5 Comments
 
LVL 5

Expert Comment

by:tgannetts
ID: 10887022
0
 
LVL 7

Accepted Solution

by:
ramesh12 earned 125 total points
ID: 10891759
0
 
LVL 4

Expert Comment

by:MajinLoki
ID: 10892734
I've done this.  Here's how I did it with a sql connection.

Dim connect As sql.SqlConnection
connect = new SqlConnection("connectionString")
connect.Open()

'get the server name
srvr = connect.DataSource()

now, if you know the location of the folder, either by default, or by letting hte user select it by using a dialog, you can use regular expressions to find each folder and then rebuild the UNC

"\\" & srvr & "\foldername"

Hope that helps.

Majin Loki
0
 
LVL 4

Author Comment

by:eeyore7250
ID: 10974133
Public Class clsUnc

    Public Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, ByRef cbRemoteName As Integer) As Integer

    Public Shared Function fnGetUncPath(ByVal strDriveLetter As String) As String
        Dim intRet As Integer
        Dim strRet As String = New String(" ", 260)
        Dim intLen As Integer = 260
        intRet = WNetGetConnection(strDriveLetter.Substring(0, 1) & ":", strRet, intLen)
        Return strRet
    End Function
End Class
0
 
LVL 4

Author Comment

by:eeyore7250
ID: 10992074
Actually this code is better:

Public Class clsUnc

    Private Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, ByRef cbRemoteName As Integer) As Integer

    Friend Shared Function fnGetUncPath(ByVal strDriveLetter As String) As String
        Dim intRet As Integer
        Dim strRet As String = ""
        Dim strTmp As String = New String(" ", 260)
        Dim intLen As Integer = 260
        Dim intCnt As Integer = 0
        intRet = WNetGetConnection(strDriveLetter.Substring(0, 1) & ":", strTmp, intLen)
        Do Until intCnt = (intLen - 1) Or strTmp.Substring(intCnt + 1, 1) = " "
            strRet &= strTmp.Substring(intCnt, 1)
            intCnt += 1
        Loop
        Return strRet
    End Function
End Class
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question