FileSystemWatcher文件文件夹监听
2007-06-25 12:49
471 查看
对指定文件夹监听,捕获新建,更改等操作。
1.新建TextBox,设为支持多行,Name=TextBoxInfo
2.新建两个按钮,开始和终了,分别表示开始监听和停止监听
3.添加处理,
Private watcher As System.IO.FileSystemWatcher = Nothing
'Button1のClickイベントハンドラ
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If Not (watcher Is Nothing) Then
Return
End If
watcher = New System.IO.FileSystemWatcher
'監視するディレクトリを指定
watcher.Path = "D:Temp"
'最終アクセス日時、最終更新日時、ファイル、フォルダ名の変更を監視する
watcher.NotifyFilter = System.IO.NotifyFilters.LastAccess Or _
System.IO.NotifyFilters.LastWrite Or _
System.IO.NotifyFilters.FileName Or _
System.IO.NotifyFilters.DirectoryName
'すべてのファイルを監視
watcher.Filter = ""
'UIのスレッドにマーシャリングする
'コンソールアプリケーションでの使用では必要ない
watcher.SynchronizingObject = Me
'イベントハンドラの追加
AddHandler watcher.Changed, AddressOf watcher_Changed
AddHandler watcher.Created, AddressOf watcher_Changed
AddHandler watcher.Deleted, AddressOf watcher_Changed
AddHandler watcher.Renamed, AddressOf watcher_Renamed
'監視を開始する
watcher.EnableRaisingEvents = True
Console.WriteLine("監視を開始しました。")
If Me.TextBoxInfo.Text = "" Then
Me.TextBoxInfo.Text = CurrentTime() & "監視を開始しました。"
Else
Me.TextBoxInfo.Text = Me.TextBoxInfo.Text & ControlChars.CrLf & CurrentTime() & "監視を開始しました。"
End If
SetScrollBarPosition()
End Sub
'Button2のClickイベントハンドラ
Private Sub Button2_Click()Sub Button2_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button2.Click
'監視を終了
watcher.EnableRaisingEvents = False
watcher.Dispose()
watcher = Nothing
Console.WriteLine("監視を終了しました。")
Me.TextBoxInfo.Text = Me.TextBoxInfo.Text & ControlChars.CrLf & CurrentTime() & "監視を開始しました。"
SetScrollBarPosition()
End Sub
'イベントハンドラ
Private Sub watcher_Changed()Sub watcher_Changed(ByVal source As System.Object, _
ByVal e As System.IO.FileSystemEventArgs)
Select Case e.ChangeType
Case System.IO.WatcherChangeTypes.Changed
Console.WriteLine(("ファイル 「" + e.FullPath + _
"」が変更されました。"))
Me.TextBoxInfo.Text = Me.TextBoxInfo.Text & ControlChars.CrLf & CurrentTime() & "ファイル 「" & e.FullPath & "」が変更されました。"
Case System.IO.WatcherChangeTypes.Created
Console.WriteLine(("ファイル 「" + e.FullPath + _
"」が作成されました。"))
Me.TextBoxInfo.Text = Me.TextBoxInfo.Text & ControlChars.CrLf & CurrentTime() & "ファイル 「" & e.FullPath & "」が作成されました。"
Case System.IO.WatcherChangeTypes.Deleted
Console.WriteLine(("ファイル 「" + e.FullPath + _
"」が削除されました。"))
Me.TextBoxInfo.Text = Me.TextBoxInfo.Text & ControlChars.CrLf & CurrentTime() & "ファイル 「" & e.FullPath & "」が削除されました。"
End Select
SetScrollBarPosition()
End Sub
Private Sub watcher_Renamed()Sub watcher_Renamed(ByVal source As System.Object, _
ByVal e As System.IO.RenamedEventArgs)
Console.WriteLine(("ファイル 「" + e.FullPath + _
"」の名前が変更されました。"))
Me.TextBoxInfo.Text = Me.TextBoxInfo.Text & ControlChars.CrLf & CurrentTime() & "ファイル 「" & e.FullPath & "」の名前が変更されました。。"
SetScrollBarPosition()
End Sub
Private Sub SetScrollBarPosition()Sub SetScrollBarPosition()
'カレット位置を末尾に移動
TextBoxInfo.SelectionStart = TextBoxInfo.Text.Length
'テキストボックスにフォーカスを移動
TextBoxInfo.Focus()
'カレット位置までスクロール
TextBoxInfo.ScrollToCaret()
End Sub
Private Function CurrentTime()Function CurrentTime() As String
Return Now.ToString("yyyy/MM/dd hh:mm:ss FFF") & " "
End Function
1.新建TextBox,设为支持多行,Name=TextBoxInfo
2.新建两个按钮,开始和终了,分别表示开始监听和停止监听
3.添加处理,
Private watcher As System.IO.FileSystemWatcher = Nothing
'Button1のClickイベントハンドラ
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If Not (watcher Is Nothing) Then
Return
End If
watcher = New System.IO.FileSystemWatcher
'監視するディレクトリを指定
watcher.Path = "D:Temp"
'最終アクセス日時、最終更新日時、ファイル、フォルダ名の変更を監視する
watcher.NotifyFilter = System.IO.NotifyFilters.LastAccess Or _
System.IO.NotifyFilters.LastWrite Or _
System.IO.NotifyFilters.FileName Or _
System.IO.NotifyFilters.DirectoryName
'すべてのファイルを監視
watcher.Filter = ""
'UIのスレッドにマーシャリングする
'コンソールアプリケーションでの使用では必要ない
watcher.SynchronizingObject = Me
'イベントハンドラの追加
AddHandler watcher.Changed, AddressOf watcher_Changed
AddHandler watcher.Created, AddressOf watcher_Changed
AddHandler watcher.Deleted, AddressOf watcher_Changed
AddHandler watcher.Renamed, AddressOf watcher_Renamed
'監視を開始する
watcher.EnableRaisingEvents = True
Console.WriteLine("監視を開始しました。")
If Me.TextBoxInfo.Text = "" Then
Me.TextBoxInfo.Text = CurrentTime() & "監視を開始しました。"
Else
Me.TextBoxInfo.Text = Me.TextBoxInfo.Text & ControlChars.CrLf & CurrentTime() & "監視を開始しました。"
End If
SetScrollBarPosition()
End Sub
'Button2のClickイベントハンドラ
Private Sub Button2_Click()Sub Button2_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button2.Click
'監視を終了
watcher.EnableRaisingEvents = False
watcher.Dispose()
watcher = Nothing
Console.WriteLine("監視を終了しました。")
Me.TextBoxInfo.Text = Me.TextBoxInfo.Text & ControlChars.CrLf & CurrentTime() & "監視を開始しました。"
SetScrollBarPosition()
End Sub
'イベントハンドラ
Private Sub watcher_Changed()Sub watcher_Changed(ByVal source As System.Object, _
ByVal e As System.IO.FileSystemEventArgs)
Select Case e.ChangeType
Case System.IO.WatcherChangeTypes.Changed
Console.WriteLine(("ファイル 「" + e.FullPath + _
"」が変更されました。"))
Me.TextBoxInfo.Text = Me.TextBoxInfo.Text & ControlChars.CrLf & CurrentTime() & "ファイル 「" & e.FullPath & "」が変更されました。"
Case System.IO.WatcherChangeTypes.Created
Console.WriteLine(("ファイル 「" + e.FullPath + _
"」が作成されました。"))
Me.TextBoxInfo.Text = Me.TextBoxInfo.Text & ControlChars.CrLf & CurrentTime() & "ファイル 「" & e.FullPath & "」が作成されました。"
Case System.IO.WatcherChangeTypes.Deleted
Console.WriteLine(("ファイル 「" + e.FullPath + _
"」が削除されました。"))
Me.TextBoxInfo.Text = Me.TextBoxInfo.Text & ControlChars.CrLf & CurrentTime() & "ファイル 「" & e.FullPath & "」が削除されました。"
End Select
SetScrollBarPosition()
End Sub
Private Sub watcher_Renamed()Sub watcher_Renamed(ByVal source As System.Object, _
ByVal e As System.IO.RenamedEventArgs)
Console.WriteLine(("ファイル 「" + e.FullPath + _
"」の名前が変更されました。"))
Me.TextBoxInfo.Text = Me.TextBoxInfo.Text & ControlChars.CrLf & CurrentTime() & "ファイル 「" & e.FullPath & "」の名前が変更されました。。"
SetScrollBarPosition()
End Sub
Private Sub SetScrollBarPosition()Sub SetScrollBarPosition()
'カレット位置を末尾に移動
TextBoxInfo.SelectionStart = TextBoxInfo.Text.Length
'テキストボックスにフォーカスを移動
TextBoxInfo.Focus()
'カレット位置までスクロール
TextBoxInfo.ScrollToCaret()
End Sub
Private Function CurrentTime()Function CurrentTime() As String
Return Now.ToString("yyyy/MM/dd hh:mm:ss FFF") & " "
End Function
相关文章推荐
- C# FileSystemWatcher 在监控文件夹和文件时的用法
- C# FileSystemWatcher 在监控文件夹和文件时的用法
- C# FileSystemWatcher 在监控文件夹和文件时的用法
- C# FileSystemWatcher 在监控文件夹和文件时的用法
- C# FileSystemWatcher监听文件事件
- C# FileSystemWatcher 在监控文件夹和文件时的用法
- C# FileSystemWatcher 在监控文件夹和文件时的用法
- C# FileSystemWatcher 在监控文件夹和文件时的用法
- C# FileSystemWatcher 在监控文件夹和文件时的用法
- C# FileSystemWatcher 在监控文件夹和文件时的用法概述
- C# FileSystemWatcher 在监控文件夹和文件时的用法
- 使用FileSystemWatcher监控文件夹及文件
- FileSystemWatcher监听文件是否有被修改
- 使用windows服务和.NET FileSystemWatcher对象来监控磁盘文件目录的改变
- C#使用FileSystemWatcher控件实现的文件监控功能示例
- C#中FileSystemWatcher控件实现文件监控
- 利用FileSystemWatcher监控文件的更改,并发送邮件通知
- 使用文件监控对象FileSystemWatcher实现数据同步
- 使用windows服务和.NET FileSystemWatcher对象来监控磁盘文件目录的改变
- FileSystemWatcher--检测系统文件更新