VB.NET代码将共享的本地路径转换为UNC路径 [英] VB.NET code to Convert shared local path to UNC path
问题描述
我们有一个名为pc2的windows 2003
服务器PC,其中本地驱动器使用不同的名称共享.例如,本地驱动器E与名称数据路径"共享,以便network
中的所有用户都使用网络路径"\\pc2\datapath\'
"访问该驱动器.我们需要VB.net代码将本地路径转换为共享的UNC path
,即,如果输入'E:\ netuse',则代码必须返回'\\pc2\datapath\netuse'
.
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'
.
在VB.net
中有什么方法可以做到这一点?
Is there any way to do this in VB.net
?
驱动器E未映射,只是共享的
Drive E is not mapped,it is just shared
推荐答案
此代码效果很好
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代码将共享的本地路径转换为UNC路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!