【问题标题】:VB.NET code to Convert shared local path to UNC pathVB.NET 代码将共享本地路径转换为 ​​UNC 路径
【发布时间】:2013-04-24 07:16:23
【问题描述】:

我们有名为 pc2 的 windows 2003 服务器 Pc,其中本地驱动器以不同的名称共享。例如,本地驱动器 E 与名称“datapath”共享,以便network 中的所有用户使用网络路径“\\pc2\datapath\'”访问该驱动器。我们需要 VB.net 代码将本地路径转换为共享的UNC path,即如果我们输入'E:\netuse',代码必须返回'\\pc2\datapath\netuse'

VB.net 有没有办法做到这一点?

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

【问题讨论】:

    标签: vb.net unc shared-directory


    【解决方案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
    

    希望这会有所帮助。

    【讨论】:

    • 我认为驱动器必须被映射才能使用上面的代码。正如我已经提到的,我们的驱动器只共享而不是映射
    • 是的刚刚试过..不能使用本地驱动器,必须使用sql server的映射驱动器错误来了
    • 当我错误地发送“A::\”作为我的驱动器而不是“A:\”(我在代码中生成字符串)时收到此消息 - 当我修复字符串时,令人困惑对 sql server 的引用消失了
    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 2011-01-19
      • 2013-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多