Chương trình con dưới đây phản ứng khi một tập tin mới được thêm vào thư mục theo dõi, lấy tên của nó bằng cách sử dụng các thông số đã được cung cấp. Hình 9-5 cho thấy mã này trong hành động. | Add an event handler dynamically. AddHandler AddressOf NewFile The subroutine below reacts when a new file is added to the monitored directory retrieving its name using the supplied parameters. Figure 9-5 shows this code in action. Public Sub NewFile ByVal sender As Object _ ByVal e As New file End Sub Figure 9-5 Monitoring the file system NOTE To make this example more robust you need a dash of multithreading smarts. That s because the FileWatcher fires events on another thread not the thread that s controlling your application. To prevent a conflict consider the code shown in the FileWatcher project included with the sample code which uses method. This technique is explained in detail in Chapter 11. File System Change Events It s easy to handle the Created Deleted and Renamed events of the FileSystemWatcher. However the Changed event is a little trickier because there are a huge number of types of possible changes that can be detected as Windows performs its ordinary housekeeping. In order to handle the Change event properly you need to set the types of changes you want to monitor in the NotifyFilter property specifying each one using a value from the NotifyFilters enumeration . You can combine several different types of monitored actions using the bitwise Or keyword. Dealing with Data Files Printing and XML 289 Watch for changes to file size or file name. Or Object Serialization The Bob program we looked at earlier used a relatively crude but effective mode of handmade serialization. The .NET platform also introduces an automatic form of serialization that you can use to store the information in an object see Figure 9-6 . This technique can be used in your own applications and the .NET Framework also relies on it to transmit a class to a remote component on another computer if you are designing a distributed .