1+
2+ namespace Demo
3+ {
4+ public partial class Form1 : Form
5+ {
6+ private FileWatcherEx _fw = new ( ) ;
7+ public Form1 ( )
8+ {
9+ InitializeComponent ( ) ;
10+ }
11+
12+
13+
14+ private void btnStart_Click ( object sender , EventArgs e )
15+ {
16+ _fw = new FileWatcherEx ( txtPath . Text . Trim ( ) ) ;
17+
18+ _fw . OnRenamed += FW_OnRenamed ;
19+ _fw . OnCreated += FW_OnCreated ;
20+ _fw . OnDeleted += FW_OnDeleted ;
21+ _fw . OnChanged += FW_OnChanged ;
22+ _fw . OnError += FW_OnError ;
23+
24+ _fw . SynchronizingObject = this ;
25+
26+ _fw . Start ( ) ;
27+
28+ btnStart . Enabled = false ;
29+ btnSelectFolder . Enabled = false ;
30+ txtPath . Enabled = false ;
31+ btnStop . Enabled = true ;
32+ }
33+
34+ private void FW_OnError ( object sender , ErrorEventArgs e )
35+ {
36+ if ( txtConsole . InvokeRequired )
37+ {
38+ txtConsole . Invoke ( FW_OnError , sender , e ) ;
39+ }
40+ else
41+ {
42+ txtConsole . Text += "[ERROR]: " + e . GetException ( ) . Message + "\r \n " ;
43+ }
44+ }
45+
46+ private void FW_OnChanged ( object sender , FileChangedEvent e )
47+ {
48+ txtConsole . Text += string . Format ( "[cha] {0} | {1}" ,
49+ Enum . GetName ( typeof ( ChangeType ) , e . ChangeType ) ,
50+ e . FullPath ) + "\r \n " ;
51+ }
52+
53+ private void FW_OnDeleted ( object sender , FileChangedEvent e )
54+ {
55+ txtConsole . Text += string . Format ( "[del] {0} | {1}" ,
56+ Enum . GetName ( typeof ( ChangeType ) , e . ChangeType ) ,
57+ e . FullPath ) + "\r \n " ;
58+ }
59+
60+ private void FW_OnCreated ( object sender , FileChangedEvent e )
61+ {
62+ txtConsole . Text += string . Format ( "[cre] {0} | {1}" ,
63+ Enum . GetName ( typeof ( ChangeType ) , e . ChangeType ) ,
64+ e . FullPath ) + "\r \n " ;
65+ }
66+
67+ private void FW_OnRenamed ( object sender , FileChangedEvent e )
68+ {
69+ txtConsole . Text += string . Format ( "[ren] {0} | {1} ----> {2}" ,
70+ Enum . GetName ( typeof ( ChangeType ) , e . ChangeType ) ,
71+ e . OldFullPath ,
72+ e . FullPath ) + "\r \n " ;
73+ }
74+
75+
76+
77+ private void btnStop_Click ( object sender , EventArgs e )
78+ {
79+ _fw . Stop ( ) ;
80+
81+ btnStart . Enabled = true ;
82+ btnSelectFolder . Enabled = true ;
83+ txtPath . Enabled = true ;
84+ btnStop . Enabled = false ;
85+ }
86+
87+
88+ private void btnSelectFolder_Click ( object sender , EventArgs e )
89+ {
90+ var fb = new FolderBrowserDialog ( ) ;
91+
92+
93+ if ( fb . ShowDialog ( ) == DialogResult . OK )
94+ {
95+ txtPath . Text = fb . SelectedPath ;
96+
97+ _fw . Stop ( ) ;
98+ _fw . Dispose ( ) ;
99+ }
100+ }
101+
102+ private void Form1_FormClosing ( object sender , FormClosingEventArgs e )
103+ {
104+ _fw . Stop ( ) ;
105+ _fw . Dispose ( ) ;
106+ }
107+ }
108+ }
0 commit comments