一般手动连结网芳的档案服务器是使用 [开启\执行] 输入要连结的计算机,例如输入 "\\FileServer",然后输入用户名称及密码进行登入。若要用程序来完成此动作,需要透过 WNetCancelConnection2 API 函式完成,以下范例就是示范如何连结档案伺服及中断档案服务器。
TAPIMpr 类别为操作 mpr.dll 函式库类别
1: ''' <summary>
2: ''' mpr.dll 函式库。
3: ''' </summary>
4: ''' <remarks></remarks>
5: Public NotInheritable Class TAPIMpr
6: Public Enum EResourceScope
7: RESOURCE_CONNECTED = 1
8: RESOURCE_GLOBALNET = 2
9: RESOURCE_REMEMBERED = 3
10: RESOURCE_RECENT = 4
11: RESOURCE_CONTEXT = 5
12: End Enum
13:
14: Public Enum EResourceType
15: RESOURCETYPE_ANY = 0
16: RESOURCETYPE_DISK = 1
17: RESOURCETYPE_PRINT = 2
18: RESOURCETYPE_RESERVED = 8
19: RESOURCETYPE_UNKNOWN = &HFFFFFFFF
20: End Enum
21:
22: Public Enum EResourceDisplayType
23: RESOURCEDISPLAYTYPE_GENERIC = 0
24: RESOURCEDISPLAYTYPE_DOMAIN = 1
25: RESOURCEDISPLAYTYPE_SERVER = 2
26: RESOURCEDISPLAYTYPE_SHARE = 3
27: RESOURCEDISPLAYTYPE_FILE = 4
28: RESOURCEDISPLAYTYPE_GROUP = 5
29: RESOURCEDISPLAYTYPE_NETWORK = 6
30: RESOURCEDISPLAYTYPE_ROOT = 7
31: RESOURCEDISPLAYTYPE_SHAREADMIN = 8
32: RESOURCEDISPLAYTYPE_DIRECTORY = 9
33: RESOURCEDISPLAYTYPE_TREE = 10
34: RESOURCEDISPLAYTYPE_NDSCONTAINER = 11
35: End Enum
36:
37: Public Enum EResourceUsage
38: RESOURCEUSAGE_CONNECTABLE = 1
39: RESOURCEUSAGE_CONTAINER = 2
40: RESOURCEUSAGE_NOLOCALDEVICE = 4
41: RESOURCEUSAGE_SIBLING = 8
42: RESOURCEUSAGE_ATTACHED = 10
43: RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE Or RESOURCEUSAGE_CONTAINER Or RESOURCEUSAGE_ATTACHED)
44: RESOURCEUSAGE_RESERVED = &H80000000
45: End Enum
46:
47: <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
48: Public Class TNetResource
49: Public dwScope As EResourceScope
50: Public dwType As EResourceType
51: Public dwDisplayType As EResourceDisplayType
52: Public dwUsage As EResourceUsage
53: <MarshalAs(UnmanagedType.LPTStr)> _
54: Public LocalName As String
55: <MarshalAs(UnmanagedType.LPTStr)> _
56: Public RemoteName As String
57: <MarshalAs(UnmanagedType.LPTStr)> _
58: Public Comment As String
59: <MarshalAs(UnmanagedType.LPTStr)> _
60: Public Provider As String
61: End Class
62:
63: <DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
64: Public Shared Function WNetAddConnection2(ByVal NetResource As TNetResource, ByVal Password As String, ByVal UserName As String, ByVal Flags As Integer) As Integer
65: End Function
66:
67: <DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
68: Public Shared Function WNetCancelConnection2(ByVal Name As String, ByVal Flags As Integer, ByVal Force As Boolean) As Integer
69: End Function
70:
71: End Class
接下来撰写 FileServerConnect 及 FileServerDisconnect 函式,用来连结档案服务器及中断档案服务器连结。
1: ''' <summary>
2: ''' 连结档案服务器。
3: ''' </summary>
4: ''' <param name="FileServer">档案服务器。</param>
5: ''' <param name="UserName">登入用户。</param>
6: ''' <param name="Password">登入密码。</param>
7: ''' <returns>连结成功传回 True,反之传回 False。</returns>
8: Public Shared Function FileServerConnect(ByVal FileServer As String, ByVal UserName As String, ByVal Password As String) As Boolean
9: Dim oNetResource As New TAPIMpr.TNetResource()
10: Dim iResult As Integer
11:
12: oNetResource.dwScope = TAPIMpr.EResourceScope.RESOURCE_GLOBALNET
13: oNetResource.dwDisplayType = TAPIMpr.EResourceDisplayType.RESOURCEDISPLAYTYPE_SHARE
14: oNetResource.dwUsage = TAPIMpr.EResourceUsage.RESOURCEUSAGE_CONNECTABLE
15: oNetResource.dwType = TAPIMpr.EResourceType.RESOURCETYPE_DISK
16: oNetResource.LocalName = String.Empty
17: oNetResource.RemoteName = "\\" & FileServer
18: iResult = API.TAPIMpr.WNetAddConnection2(oNetResource, Password, UserName, 0)
19:
20: Return iResult = 0
21: End Function
22:
23: ''' <summary>
24: ''' 中断档案服务器连结。
25: ''' </summary>
26: ''' <param name="FileServer">档案服务器。</param>
27: ''' <returns>成功传回 True,反之传回 False。</returns>
28: Public Shared Function FileServerDisconnect(ByVal FileServer As String) As Boolean
29: Dim iResult As Integer
30:
31: iResult = TAPIMpr.WNetCancelConnection2("\\" & FileServer, 0, True)
32: Return iResult = 0
33: End Function
使用 FileServerConnect 及 FileServerDisconnect 函式的方式如下
1: If FileServerConnect("FileServer", "UserName", "Password") Then
2: MsgBox("连结档案服务器成功!")
3: End If
4:
5: If FileServerDisconnect("FileServer") Then
6: MsgBox("中断档案服务器连结成功!")
7: End If