Vb.net 以编程方式映射网络驱动器

Vb.net 以编程方式映射网络驱动器,vb.net,path,Vb.net,Path,我有VB.NET代码,用于将驱动器映射到网络路径 strPath = "\\11.22.33.11\Hostsw\Host\SW\" 当我使用下面的函数调用MapDrive(“T”、strpath、“pcs”和“$pcspcs$”)时,它会出现错误,并显示消息“坏路径无法连接到星型目录” 研究解决方案是首先查看方法的p/Invoke定义 并确保方法定义正确无误。例如,使用Integer而不是UInt32等等 一个快速而肮脏但有效的解决方案是不要重新发明轮子,而是使用每个Windows安

我有VB.NET代码,用于将驱动器映射到网络路径

strPath = "\\11.22.33.11\Hostsw\Host\SW\"  
当我使用下面的函数调用
MapDrive(“T”、strpath、“pcs”和“$pcspcs$”)
时,它会出现错误,并显示消息
“坏路径无法连接到星型目录”


研究解决方案是首先查看方法的p/Invoke定义


并确保方法定义正确无误。例如,使用
Integer
而不是
UInt32
等等

一个快速而肮脏但有效的解决方案是不要重新发明轮子,而是使用每个Windows安装中包含的
net
工具:

Module Module1
Public Sub MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String, ByVal strUsername As String, ByVal strPassword As String)
    Dim p As New Process()
    p.StartInfo.FileName = "net.exe"
    p.StartInfo.Arguments = " use " & DriveLetter & ": " & UNCPath & " " & strPassword & " /USER:" & strUsername
    p.StartInfo.CreateNoWindow = True
    p.Start()
    p.WaitForExit()
End Sub

Sub Main()
    MapDrive("x", "\\FoosServer\FoosShare", "FoosServer\Bob", "correcthorsebatterystaple")
End Sub

End Module
代码的作用是,它使用正确的参数(例如
net use x:\\Server\share password/USER:domain\Username
)运行
net.exe
(路径包含在
path
环境变量中,因此无需包含它),然后映射网络驱动器

Module Module1
Public Sub MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String, ByVal strUsername As String, ByVal strPassword As String)
    Dim p As New Process()
    p.StartInfo.FileName = "net.exe"
    p.StartInfo.Arguments = " use " & DriveLetter & ": " & UNCPath & " " & strPassword & " /USER:" & strUsername
    p.StartInfo.CreateNoWindow = True
    p.Start()
    p.WaitForExit()
End Sub

Sub Main()
    MapDrive("x", "\\FoosServer\FoosShare", "FoosServer\Bob", "correcthorsebatterystaple")
End Sub

End Module