Solved

Get UNC path of a network drive

Posted on 2004-04-21
5
1,084 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now