連結檔案伺服器

  • 4350
  • 0
  • API
  • 2008-04-22

連結檔案伺服器

一般手動連結網芳的檔案伺服器是使用 [開啟\執行] 輸入要連結的電腦,例如輸入 \\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

 

ASP.NET 魔法學院