Solved

Get UNC path of a network drive

Posted on 2004-04-21
5
1,092 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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