[VB.NET]偵測是否有卸除式存放裝置插入,使用 DriveInfo 類別

  • 14653
  • 0
  • 2010-08-02

偵測是否有卸除式存放裝置插入,使用 DriveInfo 類別

 

1. 問題描述

如何偵測是否有卸除式存放裝置插入

 

2. 方法

2.1 作法說明

透過 DriveInfo 類別 : 提供對磁碟上資訊的存取。 

而 DriveInfo.DriverType 可以取得磁碟類型,包括 CDRom、Fixed、Network、NoRootDirectory、Ram、Removable 和 Unknown。

DriveType 列舉型別

未命名

 

藉由上述之類別,透過 Timer 定時取得電腦有哪些磁碟,以及取得磁碟類型,當有磁碟類型為 Removalbe 時,將磁碟名稱顯示出來

 

2.2 程式碼


Imports System.IO
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 100
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ListBox1.Items.Clear()
        Dim di As DriveInfo
        For Each di In DriveInfo.GetDrives
            If di.DriveType = DriveType.Removable Then
                ListBox1.Items.Add("偵測到" & di.Name & "抽取式存放裝置")
            End If
        Next
    End Sub
End Class

 

2.3 執行結果

插入抽取式存放裝置時

image

拔除抽取式存放裝置時

image

 

2.4 範例下載

 DetectDisk.rar

 

3. 參考

攔截磁碟IO