ファイル監視(C#/VB.NET)
2011年08月06日
フォルダ内のファイル(フォルダ)の更新を監視するサンプル(ソース/コード)です。その他のファイル監視サンプルやファイル監視ツールはこちら「ファイル監視/フォルダ監視」です。
' ----------------------------------------------------------
' フォルダ内のファイル更新を監視するサンプル(VB.NET/VS2005)
Private fchk As New System.IO.FileSystemWatcher
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' 監視停止
fchk.EnableRaisingEvents = False
' 監視フォルダ
fchk.Path = "D:\Temp\Test"
' 監視項目(アクセス日時、更新日時、名称変更
fchk.NotifyFilter = _
System.IO.NotifyFilters.LastAccess Or _
System.IO.NotifyFilters.LastWrite Or _
System.IO.NotifyFilters.FileName Or _
System.IO.NotifyFilters.DirectoryName
' フィルタ(拡張子等:*.txt)
fchk.Filter = ""
' サブフォルダ監視有無
fchk.IncludeSubdirectories = True
' 同期オブジェクト
fchk.SynchronizingObject = Me
' イベント定義
AddHandler fchk.Created, AddressOf fchk_sub
AddHandler fchk.Deleted, AddressOf fchk_sub
AddHandler fchk.Changed, AddressOf fchk_sub
AddHandler fchk.Renamed, AddressOf fchk_sub
' 監視開始
fchk.EnableRaisingEvents = True
End Sub
Private Sub fchk_sub( _
ByVal source As Object, _
ByVal e As System.IO.FileSystemEventArgs)
Select Case e.ChangeType
Case System.IO.WatcherChangeTypes.Created
Debug.WriteLine("作成:" + e.FullPath)
Case System.IO.WatcherChangeTypes.Deleted
Debug.WriteLine("削除:" + e.FullPath)
Case System.IO.WatcherChangeTypes.Changed
Debug.WriteLine("変更:" + e.FullPath)
End Select
End Sub
Private Sub fchk_sub( _
ByVal source As Object, _
ByVal e As System.IO.RenamedEventArgs)
Select Case e.ChangeType
Case System.IO.WatcherChangeTypes.Renamed
Debug.WriteLine("名称:" + e.FullPath)
End Select
End Sub
' ----------------------------------------------------------
// ---------------------------------------------------------
// フォルダ内のファイル更新を監視するサンプル(C#.NET/VS2005)
private System.IO.FileSystemWatcher fchk =
new System.IO.FileSystemWatcher();
private void button1_Click(object sender, EventArgs e)
{
// 監視停止
fchk.EnableRaisingEvents = false;
// 監視フォルダ
fchk.Path = @"D:\Temp\Test";
// 監視項目(アクセス日時、更新日時、名称変更
fchk.NotifyFilter =
System.IO.NotifyFilters.LastAccess |
System.IO.NotifyFilters.LastWrite |
System.IO.NotifyFilters.FileName |
System.IO.NotifyFilters.DirectoryName;
// フィルタ(拡張子等:*.txt)
fchk.Filter = "";
// サブフォルダ監視有無
fchk.IncludeSubdirectories = true;
// 同期オブジェクト
fchk.SynchronizingObject = this;
// イベント定義
fchk.Created +=
new System.IO.FileSystemEventHandler(fchk_sub);
fchk.Deleted +=
new System.IO.FileSystemEventHandler(fchk_sub);
fchk.Changed +=
new System.IO.FileSystemEventHandler(fchk_sub);
fchk.Renamed +=
new System.IO.RenamedEventHandler(fchk_sub);
// 監視開始
fchk.EnableRaisingEvents = true;
}
private void fchk_sub(
System.Object source,
System.IO.FileSystemEventArgs e)
{
switch (e.ChangeType)
{
case System.IO.WatcherChangeTypes.Created:
Debug.WriteLine("作成:" + e.FullPath);
break;
case System.IO.WatcherChangeTypes.Deleted:
Debug.WriteLine("削除:" + e.FullPath);
break;
case System.IO.WatcherChangeTypes.Changed:
Debug.WriteLine("変更:" + e.FullPath);
break;
}
}
private void fchk_sub(
System.Object source,
System.IO.RenamedEventArgs e)
{
switch (e.ChangeType)
{
case System.IO.WatcherChangeTypes.Renamed:
Debug.WriteLine("名称:" + e.FullPath);
break;
}
}
// ---------------------------------------------------------
スポンサーサイト