Posted on 2009-07-09 09:47
Hero 阅读(471)
评论(0) 编辑 收藏 引用 所属分类:
C#积累
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
9
using System.IO;
10![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
11
namespace FileWatch
12![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
13
public partial class Form1 : Form
14![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
//FileSystemWatcher object
16
private FileSystemWatcher filewatcher;
17
//声明委托
18
private delegate void UpdateWatchTextDelegate( string newText );
19![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
20
public Form1()
21![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
InitializeComponent();
23![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
24
this.filewatcher = new FileSystemWatcher();
25![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
26
//添加事件订阅
27
this.filewatcher.Deleted += new FileSystemEventHandler(filewatcher_Deleted);
28
this.filewatcher.Renamed += new RenamedEventHandler(filewatcher_Renamed);
29
this.filewatcher.Changed += new FileSystemEventHandler(filewatcher_Changed);
30
this.filewatcher.Created += new FileSystemEventHandler(filewatcher_Created);
31![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
32
//检测文件目录是否存在
33
DirectoryInfo dirInfo = new DirectoryInfo( @"C:\FileLogs" ) ;
34![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if( !dirInfo.Exists )
{ dirInfo.Create() ; }
35
}
36![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
37
//异步更新labelWatch中的文本
38
public void UpdateWatchText( string newText )
39![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40
labelWatch.Text = newText;
41
}
42![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
43
//定义事件处理器
44
public void filewatcher_Changed( object source, FileSystemEventArgs e )
45![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
46
try
47![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
StreamWriter streamwriter = new StreamWriter( @"C:\FileLogs\Log.txt", true ) ;
49
streamwriter.WriteLine( "File: {0} {1}", e.FullPath, e.ChangeType.ToString() ) ;
50![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
51
streamwriter.Close() ;
52![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
53
this.BeginInvoke( new UpdateWatchTextDelegate(UpdateWatchText), "Wrote change event to log" ) ;
54
}
55
catch (IOException ex)
56![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
57
this.BeginInvoke( new UpdateWatchTextDelegate(UpdateWatchText), "Error writing to log" ) ;
58
}
59![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
60
}
61![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
62
public void filewatcher_Renamed( object source, RenamedEventArgs e )
63![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
64
try
65![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
StreamWriter streamwrite = new StreamWriter( @"C:\FileLogs\Log.txt", true ) ;
67![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
68
streamwrite.WriteLine( "File renamed from {0} to {1}", e.OldName, e.FullPath ) ;
69![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
70
streamwrite.Close() ;
71![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
72
this.BeginInvoke( new UpdateWatchTextDelegate( UpdateWatchText ), "wrote renamed event to log" ) ;
73![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
74
}
75
catch (IOException ex)
76![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
77
this.BeginInvoke( new UpdateWatchTextDelegate(UpdateWatchText), "Error writing to log" ) ;
78
}
79
}
80![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
81
public void filewatcher_Deleted( object source, FileSystemEventArgs e )
82![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83
try
84![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
85
//打开写文件
86
StreamWriter streamwriter = new StreamWriter( @"C:\FileLogs\Log.txt", true ) ;
87
streamwriter.WriteLine( "File : {0} deleted", e.FullPath ) ;
88![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
89
streamwriter.Close() ;
90![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
91
this.BeginInvoke( new UpdateWatchTextDelegate(UpdateWatchText), "wrote delete event to log" ) ;
92![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
93
}
94
catch (IOException ex)
95![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
96
this.BeginInvoke( new UpdateWatchTextDelegate(UpdateWatchText), "Error writing to log" ) ;
97
}
98
}
99![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
100
public void filewatcher_Created( object source, FileSystemEventArgs e )
101![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
102
try
103![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
104
StreamWriter streamwriter = new StreamWriter( @"C:\FileLogs\Log.txt", true ) ;
105
streamwriter.WriteLine( "File : {0} was created", e.FullPath ) ;
106
streamwriter.Close() ;
107![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
108
this.BeginInvoke( new UpdateWatchTextDelegate( UpdateWatchText), "wrote create to log" ) ;
109
}
110
catch (IOException ex)
111![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
112
this.BeginInvoke( new UpdateWatchTextDelegate(UpdateWatchText), "Erroe writing to log" ) ;
113
}
114
}
115![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
116
private void Form1_Load( object sender, EventArgs e )
117![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
118![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
119
}
120![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
121
private void buttonBrowse_Click(object sender, EventArgs e)
122![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
123
if( openFileDialog1.ShowDialog() != DialogResult.Cancel )
124![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
125
//显示打开的文件名字
126
this.textBoxLocation.Text = openFileDialog1.FileName ;
127![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
128
//可以启动watch按钮
129
buttonWatch.Enabled = true ;
130
}
131
}
132![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
133
private void buttonWatch_Click(object sender, EventArgs e)
134![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
135
//获取监视的路径
136
filewatcher.Path = Path.GetDirectoryName( textBoxLocation.Text ) ;
137
//filewatcher.Filter = Path.GetFileName( textBoxLocation.Text ) ;
138
filewatcher.Filter = "*.txt";
139
filewatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.CreationTime | NotifyFilters.LastAccess ;
140![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
141
labelWatch.Text = "watching " + textBoxLocation.Text ;
142![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
143
//begin watching
144
filewatcher.EnableRaisingEvents = true ;
145
}
146
}
147
}