VB.NET code to Convert shared local path to UNC path

主宰稳场 提交于 2019-12-03 21:55:02

问题


We have windows 2003 server Pc named pc2 in which local drives are shared with different names. For example local drive E is shared with name 'datapath' so that all users in network access that drive using network path '\\pc2\datapath\'. We need VB.net code to convert local path to shared UNC path i.e if we input 'E:\netuse',code must return '\\pc2\datapath\netuse'.

Is there any way to do this in VB.net ?

EDIT: Drive E is not mapped,it is just shared


回答1:


Just created a small app which seems to work.

It takes a textbox with a non-unc value and converts it,then sets a label

Remember to include the following namespaces if you haven't already;

Imports System.Text
Imports System.IO

This is the method that does all the work;

Private Function GetUNCPath(ByVal sFilePath As String) As String

Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
Dim d As DriveInfo
Dim DriveType, Ctr As Integer
Dim DriveLtr, UNCName As String
Dim StrBldr As New StringBuilder

If sFilePath.StartsWith("\\") Then Return sFilePath

UNCName = Space(160)
GetUNCPath = ""

DriveLtr = sFilePath.Substring(0, 3)

For Each d In allDrives
  If d.Name = DriveLtr Then
    DriveType = d.DriveType
    Exit For
  End If
Next

If DriveType = 4 Then

  Ctr = WNetGetConnection(sFilePath.Substring(0, 2), UNCName, UNCName.Length)

  If Ctr = 0 Then
    UNCName = UNCName.Trim
    For Ctr = 0 To UNCName.Length - 1
      Dim SingleChar As Char = UNCName(Ctr)
      Dim asciiValue As Integer = Asc(SingleChar)
      If asciiValue > 0 Then
        StrBldr.Append(SingleChar)
      Else
        Exit For
      End If
    Next
    StrBldr.Append(sFilePath.Substring(2))
    GetUNCPath = StrBldr.ToString
  Else
    MsgBox("Cannot Retrieve UNC path" & vbCrLf & "Must Use Mapped Drive of SQLServer", MsgBoxStyle.Critical)
  End If
Else
  MsgBox("Cannot Use Local Drive" & vbCrLf & "Must Use Mapped Drive of SQLServer", MsgBoxStyle.Critical)
End If

End Function

Declare this function;

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

Call the code from a button click;

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
     Dim RealFileName As String = GetUNCPath(txtFileName.Text)
     lblUNC.Text = RealFileName
  End Sub

Hope this helps.




回答2:


This code worked very well

        Dim SharePath As String = "e:\Netuse"
        Dim SplitPath() As String = Split(SharePath, "\")
        Dim CurrentPath As String = String.Empty
        Dim ShareName As String = String.Empty
        Dim CurrentFolderIndex As Integer
        Dim UNCPath As String = String.Empty

        For CurrentFolderIndex = 0 To SplitPath.GetUpperBound(0)
            If CurrentPath = String.Empty Then
                CurrentPath = String.Concat(SplitPath(CurrentFolderIndex), "\\")
            Else
                CurrentPath += String.Concat(SplitPath(CurrentFolderIndex), "\\")
            End If
            ShareName = GetShareName(CurrentPath)
            If ShareName <> String.Empty Then
                CurrentFolderIndex += 1
                Exit For
            End If
        Next

        UNCPath = String.Concat("\\", My.Computer.Name, "\", ShareName)

        For SubPathIndex As Integer = CurrentFolderIndex To SplitPath.GetUpperBound(0)
            UNCPath = String.Concat(UNCPath, "\", SplitPath(SubPathIndex))
        Next

        Console.WriteLine(UNCPath)



    Public Function GetShareName(ByVal FolderPath As String) As String

        Dim Searcher As New ManagementObjectSearcher(String.Concat("select * from win32_share WHERE Path = '", FolderPath, "'"))
        Dim ShareName As String = String.Empty

        For Each Share As ManagementObject In Searcher.Get()
            ShareName = Share("Name").ToString
        Next

        Return ShareName

    End Function


来源:https://stackoverflow.com/questions/16185716/vb-net-code-to-convert-shared-local-path-to-unc-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!