連線/中斷網路磁碟機對話框

連線/中斷網路磁碟機對話框

此範例是利用 API 來開啟「連線/中斷網路磁碟機對話框」,我們必須使用 mpr.dll API 函式庫的 WNetConnectionDialog 及 WNetDisconnectDialog 函式來完成,以下的 TAPIMpr 類別為操作 mpr.dll 函式庫類別。

 

   1:      ''' <summary>
   2:      ''' 網路芳鄰相關函式庫(mpr.dll)。
   3:      ''' </summary>
   4:      Public NotInheritable Class TAPIMpr
   5:   
   6:         Public Enum EResourceType As Integer
   7:              ''' <summary>
   8:              ''' 何任資源。
   9:              ''' </summary>
  10:              RESOURCETYPE_ANY = &H0
  11:              ''' <summary>
  12:              ''' 網路磁碟機。
  13:              ''' </summary>
  14:              RESOURCETYPE_DISK = &H1
  15:              ''' <summary>
  16:              ''' 網路印表機。
  17:              ''' </summary>
  18:              RESOURCETYPE_PRINT = &H2
  19:              ''' <summary>
  20:              ''' 未知。
  21:              ''' </summary>
  22:              RESOURCETYPE_UNKNOWN = &HFFFFFFFF
  23:          End Enum
  24:   
  25:          ''' <summary>
  26:          ''' 連線網路磁碟機對話框。
  27:          ''' </summary>
  28:          <DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
  29:          Public Shared Function WNetConnectionDialog(ByVal hwnd As IntPtr, ByVal Type As EResourceType) As Integer
  30:          End Function
  31:   
  32:          ''' <summary>
  33:          ''' 中斷網路磁碟機對話框。
  34:          ''' </summary>
  35:          <DllImport("mpr.dll", CharSet:=CharSet.Auto)> _
  36:          Public Shared Function WNetDisconnectDialog(ByVal hwnd As IntPtr, ByVal Type As EResourceType) As Integer
  37:          End Function
  38:   
  39:      End Class

 

呼叫 API 的 WNetConnectionDialog 及 WNetDisconnectDialog 函式的方式如下。

   1:      ''' <summary>
   2:      ''' 連線網路磁碟機對話框。
   3:      ''' </summary>
   4:      ''' <param name="Handle">視窗 Handle。</param>
   5:      ''' <returns>連線成功傳回 True,反之傳回 False。</returns>
   6:      Public Overloads Shared Function NetConnectionDialog(ByVal Handle As IntPtr) As Boolean
   7:          Dim iResult As Integer
   8:   
   9:          iResult = TAPIMpr.WNetConnectionDialog(Handle, TAPIMpr.EResourceType.RESOURCETYPE_DISK)
  10:          Return iResult = 0
  11:      End Function
  12:   
  13:      ''' <summary>
  14:      ''' 中斷網路磁碟機對話框。
  15:      ''' </summary>
  16:      ''' <param name="Handle">視窗 Handle。</param>
  17:      ''' <returns>成功傳回 True,反之傳回 False。</returns>
  18:      Public Overloads Shared Function NetDisconnectDialog(ByVal Handle As IntPtr) As Boolean
  19:          Dim iResult As Integer
  20:   
  21:          iResult = TAPIMpr.WNetDisconnectDialog(Handle, TAPIMpr.EResourceType.RESOURCETYPE_DISK)
  22:          Return iResult = 0
  23:      End Function

ASP.NET 魔法學院