HOW TO:設定 FileSystemWatcher 元件執行個體

更新:2007 年 11 月

您可以為 FileSystemWatcher 元件執行個體設定多個屬性,決定屬性的行為。這些屬性會決定元件執行個體將監視哪些目錄及子目錄,以及這些會引發事件之目錄內所發生的實際動作。

指定要監看的目錄

您可以使用兩個屬性,來決定 FileSystemWatcher 元件必須監看哪些目錄:PathIncludeSubdirectories

若要指出要監看之根目錄的完整路徑名稱

  • 在現有的元件執行個體上設定 Path 屬性,如下列程式碼所示:

    Dim MyWatcher As New System.IO.FileSystemWatcher()
    MyWatcher.Path = "c:\"
    
         System.IO.FileSystemWatcher MyWatcher = new System.IO.FileSystemWatcher();
            MyWatcher.Path = "c:\\";
    

    這個設定可以標準路徑標記法 (c:\directory) 或 UNC 格式 (\\server\directory) 來表示。

IncludeSubdirectories 屬性指出是否應監視主目錄內的子目錄。如果屬性設定為 true,則元件會像監視主目錄內的變更一樣來監視子目錄內的相同變更。

指定要監看的變更

除了指定要監看的目錄和子目錄之外,還可以指示您要監看哪些特定檔案和變更類型。就某種程度而言,可以只為您要的事件定義處理常式來決定要監看的變更。例如,若您只想在建立檔案時才會接獲告知,就只需處理 Created 事件。不過,您可以設定一系列的屬性來進一步限制元件的活動。

您可以使用 Filter 屬性,指示是透過特定檔案或萬用字元比對模式來限制元件只監看根目錄內的特定檔案或目錄。例如,若您要監看根目錄內文字檔的建立,則可將 Filter 屬性設定為 "*.txt",然後為 Created 事件建立處理常式。您可以將篩選條件設定為任何值,其中包括所有有效的檔案副檔名。預設值 "*.*" 會傳回所有項目;"" (空字串) 則會傳回所有的檔案或目錄。然而,您一次只能設定一個篩選條件字串。若有需要,您可以在 FileSystemWatcher 已經開始接收事件之後再變更篩選條件。

注意事項:

FileSystemWatcher 元件是設計來監視目錄內的變更,而不是目錄屬性本身的變更。例如,如果您正在監看名為 c:\MyProjects 的目錄,元件就會監視目錄內的變更,而不是目錄本身的變更。

您也可以使用 NotifyFilter 屬性,進一步限制回應的變更。您可以設定 NotifyFilter 屬性監看目錄名稱、檔案名稱或兩者的變更。除此之外,NotifyFilter 屬性還包含一系列可限制 Changed 事件的值。Changed 事件可因許多事件引發,因為它會在每次變更檔案時引發事件,就像變更檔案的大小、上次寫入及上次存取等屬性,一些像是複製和移動檔案的標準作業實際上也會導致引發幾個檔案變更事件。

NotifyFilter 屬性限制讓元件執行個體引發檔案或目錄變更事件的類型。您可以設定元件,讓元件只有當檔案或目錄名稱變更、主目錄內的項目屬性變更、檔案的大小變更、上次寫入或上次存取時間變更或是檔案或目錄的安全存取權限變更時才引發事件。這些值為 NotifyFilters 列舉型別的所有部分。您可以使用 BitOr (適用於 Visual Basic) 或 | (適用於 Visual C#) 運算子,設定多個要監看的變更,如以下範例所示。

注意事項:

您必須將 EnableRaisingEvents 屬性設定為 true,讓 FileSystemWatcher 元件能開始監看變更。

若要設定 FileSystemWatcher 元件的執行個體

  1. 建立 FileSystemWatcher 元件的執行個體。如需詳細資訊,請參閱 HOW TO:建立 FileSystemWatcher 元件執行個體

  2. Path 屬性設定為您要監看之根目錄的完整路徑。

    秘訣

    您可以輸入標準檔案路徑或 UNC 路徑。

  3. 為您的執行個體設定任何選擇性屬性。

    案例

    屬性

    若要監看根目錄內特定檔案或子目錄名稱的變更

    Filter

    萬用字元篩選運算式,可限制執行個體的監視活動

    若要監看根目錄所包含任何子目錄內的變更

    IncludeSubdirectories

    true 或false

    若要在處理 ChangedRenamedDeletedCreated 事件時,只監看檔案或子目錄的特定變更

    NotifyFilter

    NotifyFilters 列舉型別中的任何可用值

  4. EnableRaisingEvents 屬性設定為 true,啟用您的元件執行個體。

    例如,假設您要建立 FileSystemWatcher 執行個體監看名為 "reports" 的收件目錄是否建立新的 .txt 檔。同時您也要監視現有報表 (當資料變更時會動態重新執行) 的變更。您可將元件設定如下:

    ' This needs to be declared in a place where it will not go out of scope.
    ' For example, it would be a class variable in a form class.
    Dim MyWatcher As New System.IO.FileSystemWatcher()
    ' This code would go in one of the initialization methods of the class.
    MyWatcher.Path = "c:\"
    ' Watch only for changes to *.txt files.
    MyWatcher.Filter = "*.txt"
    MyWatcher.IncludeSubdirectories = False
    ' Filter for Last Write changes.
    MyWatcher.NotifyFilter = System.IO.NotifyFilters.LastWrite
    ' Example of watching more than one type of change.
    MyWatcher.NotifyFilter = _
       System.IO.NotifyFilters.LastAccess Or System.IO.NotifyFilters.Size
    ' Enable the component to begin watching for changes.
    MyWatcher.EnableRaisingEvents = True
    
         // This needs to be declared in a place where it will not go out of scope.
            // For example, it would be a class variable in a form class.
            System.IO.FileSystemWatcher MyWatcher = new System.IO.FileSystemWatcher();
            // This code would go in one of the initialization methods of the class.
            MyWatcher.Path = "c:\\";
            // Watch only for changes to *.txt files.
            MyWatcher.Filter = "*.txt";
            MyWatcher.IncludeSubdirectories = false;
            // Enable the component to begin watching for changes.
            MyWatcher.EnableRaisingEvents = true;
            // Filter for Last Write changes.
            MyWatcher.NotifyFilter = System.IO.NotifyFilters.LastWrite;
            // Example of watching more than one type of change.
            MyWatcher.NotifyFilter =
               System.IO.NotifyFilters.LastWrite | System.IO.NotifyFilters.Size;
    

請參閱

工作

HOW TO:建立 FileSystemWatcher 元件執行個體

概念

監視檔案系統事件簡介