|
|
对于文件IO来说...最让人高兴的莫过于FileSystemWatcher类...
这个类在监视文件系统...并且当目录或者文件变化时...会唤起一个事件...
当文件系统中的事件发生时...引发文件系统事件...
对Windows来说...就是对消息或者事件的反应...
它们由用户直接激活...或者由正在运行的应用程序间接激活...
这些消息实质上就是事件...它们可能是用户在一个窗体上单击按钮 / 单击"开始菜单 / 复制文件或者创建目录...
因为在VB6中所创建的程序是单线程的 ( 一般情况下 )...
侦听系统事件的发生是非常困难的...不过在VB.net中完全不用这么麻烦了...
使用多线程的方法...可以监视操作系统发生的任何类型的事件...
下面介绍一下这几天刚刚学的<文件系统事件>..
事件 唤起时间
--------------------------------------------------------------
Created 创建目录或者文件
Deleted 删除目录或者文件
Renamed 修改目录或者文件名
Changed 修改大小 / 系统属性 / 最后写入时间 / 最后访问时间或者目录以及文件的安全许可
注:FileSystemWatcher类仅适用于Windows NT4 / 2000或者XP...不适合用于Windows 9X或者Me
要监视文件系统的事件...需要创建FileSystemWatcher类的一个实例...
Dim Watcher As New System.IO.FileSystemWatcher
创建监视者之后,需要设置若干属性决定应该如何监视。属性和它们的定义如下:
* Filter:决定监视文件的通配符表达式,它可以是“*.*”,表示所有文件,或者“*.txt”,表示带有.txt扩展名的文件
* IncludeSubDirectories:布尔类型,表示在指定的路径是否包括或者排除子目录
* NotifyFilter:事件监视的枚举指示,也就是枚举的智能类型
* Path:监视的路径
设置属性之后,将EnableRaisingEvents属性设置为True,则FileSystemWatcher类被激活
=======================================================
以下是演示代码,使用的是WithEvents方法创建
这样,当单个事件被唤起时,便可以从进程中处理事件
Public Class frmMain
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'frmMain
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.MaximizeBox = False
Me.Name = "frmMain"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "FileSystemWatcher"
Me.WindowState = System.Windows.Forms.FormWindowState.Minimized
End Sub
#End Region
'Public Watcher As New System.IO.FileSystemWatcher
Public WithEvents Watcher As New System.IO.FileSystemWatcher
Public g_strPath As String = System.IO.Directory.GetCurrentDirectory & "\Test"
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If System.IO.Directory.Exists(g_strPath) = False Then
System.IO.Directory.CreateDirectory(g_strPath)
End If
Watcher.Path = g_strPath
Watcher.Filter = "*.*"
Watcher.IncludeSubdirectories = True
Watcher.NotifyFilter = IO.NotifyFilters.LastWrite
Watcher.NotifyFilter = IO.NotifyFilters.LastAccess Or IO.NotifyFilters.Size Or IO.NotifyFilters.FileName
Watcher.EnableRaisingEvents = True
End Sub
Private Sub Watcher_Changed(ByVal Sender As System.Object, ByVal EEvents As System.IO.FileSystemEventArgs) Handles Watcher.Changed
WriteLog(EEvents.Name, EEvents.ChangeType.ToString)
End Sub
Private Sub Watcher_Created(ByVal Sender As System.Object, ByVal EEvents As System.IO.FileSystemEventArgs) Handles Watcher.Created
WriteLog(EEvents.Name, EEvents.ChangeType.ToString)
End Sub
Private Sub Watcher_Deleted(ByVal Sender As System.Object, ByVal EEvents As System.IO.FileSystemEventArgs) Handles Watcher.Deleted
WriteLog(EEvents.Name, EEvents.ChangeType.ToString)
End Sub
Private Function WriteLog(ByVal strFile As String, ByVal strChange As String) As Boolean
Dim SW As System.IO.StreamWriter = New System.IO.StreamWriter(g_strPath & "\Log.txt", True)
SW.WriteLine(strFile & " - " & strChange)
SW.Close()
End Function
End Class
最后,希望对这个类有一定了解的.net高手可以提点意见
比方说这个类应用到哪种项目才能够发挥极致
或者是,这个类的一些高级使用特性等等…… |
|