注册 登录
编程论坛 VB.NET论坛

映射网络磁盘到本地,如何在映射的时候修改网络磁盘卷标?? 求大神帮忙。附能正常连接、断开的代码!

moou 发布于 2018-08-15 17:31, 4286 次点击
程序代码:

    Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer
    Public Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer
    Public Const RESOURCE_CONNECTED = &H1
    Public Const RESOURCETYPE_ANY = &H0
    Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
    Public Const RESOURCEUSAGE_CONNECTABLE = &H1
    Public Structure NETRESOURCE
        Dim dwScope As Integer
        Dim dwType As Integer
        Dim dwDisplayType As Integer
        Dim dwUsage As Integer
        Dim lpLocalName As String
        Dim lpRemoteName As String
        Dim lpComment As String
        Dim lpProvider As String
    End Structure

    '连接网络磁盘函数
    Public Function netconnect(ByVal localDrive As String, ByVal sharePath As String, ByVal userName As String, ByVal UserPassword As String, ByVal Juanbiao As String) As Boolean
        Dim NetR As New NETRESOURCE
        Dim ErrInfo As Integer
        NetR.dwScope = RESOURCE_CONNECTED
        NetR.dwType = RESOURCETYPE_ANY
        NetR.dwDisplayType = RESOURCETYPE_ANY
        NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
        '设置驱动器
        NetR.lpLocalName = localDrive
        '设置远程端口名字
        NetR.lpRemoteName = sharePath
        '这里尝试修改这个参数,看看能不能修改卷标
        NetR.lpComment = Juanbiao
        NetR.lpProvider = ""
        ErrInfo = WNetAddConnection2(NetR, UserPassword, userName, 1)
        If ErrInfo = 0 Then
            Return True
        Else
            Return False
        End If
    End Function
    '断开网络磁盘函数
    Public Function netdisconnect(ByVal localDrive As String) As Boolean
        Dim ErrInfo As Integer
        ErrInfo = WNetCancelConnection2(localDrive, 1, False)
        If ErrInfo = 0 Then
            Return True
        Else
            Return False
        End If
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Netpatch As String ="\\192.168.0.250\A001"
        Dim Username As String ="A001"
        Dim password As String ="123"
        Dim Netdisk As String ="X:"
        '这里尝试传递卷标给参数 NetR.lpComment 可是无效
        Dim Diskname As String = "个性化磁盘"

        If netconnect(Netdisk, Netpatch, Username, password, Diskname) = True Then

            MsgBox("磁盘连接成功!")
            Me.Close()
        Else
            MsgBox("磁盘连接失败!")
        End If
    End Sub



5 回复
#2
moou2018-08-15 18:11
求各位大神有空帮忙看下 谢谢
版主:xyxcc177 zhangcanwei HVB6 有空 看下啊


[此贴子已经被作者于2018-8-15 18:13编辑过]

#3
喜之浪2018-08-22 08:40
楼主问题解决没?我也遇到这个问题。还没找到解决方法。另外,你这贴是不是该 发在VB版块里呀?
#4
moou2018-08-22 11:23
回复 3楼 喜之浪
没有解决,到现在也没解决,话说我是用vb 2010写的,发到这里没错吧
#5
喜之浪2018-08-22 14:41
回复 4楼 moou
我找到了一个不是办法的办法。测试可用。修改映射网络驱动器卷标无感修改,修改本地驱动器卷标时要弹出窗口需要管理员权限。
我也是学艺不精,先将就用着。等有高手提供好代码了再修改。

程序代码:

'更改磁盘驱动器的卷标
'
pan :拟更改的磁盘驱动器,比如:z:\ ,注意带后面的斜杠
'
name:拟更改的卷标名称

Function gjb(pan, name)

    Dim DaiMa As String

    DaiMa = "Set oShell = CreateObject(" + Chr(34) + "Shell.Application" + Chr(34) + ")" + vbCrLf _
    + "oShell.NameSpace(" + Chr(34) + pan + Chr(34) + ").Self.Name = " + Chr(34) + name + Chr(34)
    Dim vbs As Object
    Set vbs = CreateObject("MSScriptControl.ScriptControl")
    vbs.Language = "VBScript"
    vbs.ExecuteStatement (DaiMa)

End Function
#6
moou2018-08-27 13:17
回复 5楼 喜之浪
谢谢兄弟,测试了下,完全没有问题
1