Solved

Get UNC path of a network drive

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

11 Experts available now in Live!

Get 1:1 Help Now