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