VB.NET代码转换共享的本地路径的UNC路径(VB.NET code to Convert sha

2019-09-01 00:11发布

我们有windows 2003服务器PC名为PC2在本地驱动器与不同的名称共享。 例如本地驱动器E与名称“数据通路”的共享,使各用户network访问该驱动器使用网络路径“ \\pc2\datapath\' 。 我们必须VB.net代码到本地路径转换为共享的UNC path ,即如果我们输入“E:\ netuse”,代码必须返回“ \\pc2\datapath\netuse'

有没有办法在做这个VB.net

编辑:驱动器E没有被映射,它只是共享

Answer 1:

刚刚创建一个小的应用程序,它似乎工作。

这需要一个文本框与非UNC值,并将其转换,则设置了一个标签

请记住,包括下面的命名空间,如果你还没有准备好;

Imports System.Text
Imports System.IO

这是做所有工作的方法;

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 Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, _
       ByVal lpszRemoteName As String, ByRef cbRemoteName As Integer) As Integer

从呼叫按钮点击代码;

  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

希望这可以帮助。



Answer 2:

此代码的工作非常出色

        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


文章来源: VB.NET code to Convert shared local path to UNC path