1+ using D2Phap . FileWatcherEx ;
12
2- using FileWatcherEx ;
3+ namespace Demo ;
34
4- namespace Demo
5+ public partial class Form1 : Form
56{
6- public partial class Form1 : Form
7+ private FileSystemWatcherEx _fw = new ( ) ;
8+
9+ public Form1 ( )
710 {
8- private FileSystemWatcherEx _fw = new ( ) ;
11+ InitializeComponent ( ) ;
12+ }
913
10- public Form1 ( )
11- {
12- InitializeComponent ( ) ;
13- }
1414
1515
16+ private void BtnStart_Click ( object sender , EventArgs e )
17+ {
18+ _fw = new FileSystemWatcherEx ( txtPath . Text . Trim ( ) , FW_OnLog ) ;
1619
17- private void BtnStart_Click ( object sender , EventArgs e )
18- {
19- _fw = new FileSystemWatcherEx ( txtPath . Text . Trim ( ) , FW_OnLog ) ;
20-
21- _fw . OnRenamed += FW_OnRenamed ;
22- _fw . OnCreated += FW_OnCreated ;
23- _fw . OnDeleted += FW_OnDeleted ;
24- _fw . OnChanged += FW_OnChanged ;
25- _fw . OnError += FW_OnError ;
26-
27- _fw . SynchronizingObject = this ;
28- _fw . IncludeSubdirectories = true ;
29-
30- try
31- {
32- _fw . Start ( ) ;
33-
34- btnStart . Enabled = true ;
35- btnSelectFolder . Enabled = false ;
36- txtPath . Enabled = false ;
37- btnStop . Enabled = true ;
38- }
39- catch ( Exception ex )
40- {
41- MessageBox . Show ( ex . Message ) ;
42- }
43- }
20+ _fw . OnRenamed += FW_OnRenamed ;
21+ _fw . OnCreated += FW_OnCreated ;
22+ _fw . OnDeleted += FW_OnDeleted ;
23+ _fw . OnChanged += FW_OnChanged ;
24+ _fw . OnError += FW_OnError ;
4425
45- private void FW_OnError ( object ? sender , ErrorEventArgs e )
46- {
47- if ( txtConsole . InvokeRequired )
48- {
49- txtConsole . Invoke ( FW_OnError , sender , e ) ;
50- }
51- else
52- {
53- txtConsole . Text += "[ERROR]: " + e . GetException ( ) . Message + "\r \n " ;
54- }
55- }
26+ _fw . SynchronizingObject = this ;
27+ _fw . IncludeSubdirectories = true ;
5628
57- private void FW_OnChanged ( object ? sender , FileChangedEvent e )
29+ try
5830 {
59- txtConsole . Text += string . Format ( "[cha] {0} | {1}" ,
60- Enum . GetName ( typeof ( ChangeType ) , e . ChangeType ) ,
61- e . FullPath ) + "\r \n " ;
62- }
31+ _fw . Start ( ) ;
6332
64- private void FW_OnDeleted ( object ? sender , FileChangedEvent e )
65- {
66- txtConsole . Text += string . Format ( "[del] {0} | {1}" ,
67- Enum . GetName ( typeof ( ChangeType ) , e . ChangeType ) ,
68- e . FullPath ) + "\r \n " ;
33+ btnStart . Enabled = true ;
34+ btnSelectFolder . Enabled = false ;
35+ txtPath . Enabled = false ;
36+ btnStop . Enabled = true ;
6937 }
70-
71- private void FW_OnCreated ( object ? sender , FileChangedEvent e )
38+ catch ( Exception ex )
7239 {
73- txtConsole . Text += string . Format ( "[cre] {0} | {1}" ,
74- Enum . GetName ( typeof ( ChangeType ) , e . ChangeType ) ,
75- e . FullPath ) + "\r \n " ;
40+ MessageBox . Show ( ex . Message ) ;
7641 }
42+ }
7743
78- private void FW_OnRenamed ( object ? sender , FileChangedEvent e )
44+ private void FW_OnError ( object ? sender , ErrorEventArgs e )
45+ {
46+ if ( txtConsole . InvokeRequired )
7947 {
80- txtConsole . Text += string . Format ( "[ren] {0} | {1} ----> {2}" ,
81- Enum . GetName ( typeof ( ChangeType ) , e . ChangeType ) ,
82- e . OldFullPath ,
83- e . FullPath ) + "\r \n " ;
48+ txtConsole . Invoke ( FW_OnError , sender , e ) ;
8449 }
85-
86- private void FW_OnLog ( string value )
50+ else
8751 {
88- txtConsole . Text += $@ "[log] { value } " + "\r \n " ;
52+ txtConsole . Text += "[ERROR]: " + e . GetException ( ) . Message + "\r \n " ;
8953 }
54+ }
9055
91- private void BtnStop_Click ( object sender , EventArgs e )
92- {
93- _fw . Stop ( ) ;
56+ private void FW_OnChanged ( object ? sender , FileChangedEvent e )
57+ {
58+ txtConsole . Text += string . Format ( "[cha] {0} | {1}" ,
59+ Enum . GetName ( e . ChangeType ) ,
60+ e . FullPath ) + "\r \n " ;
61+ }
9462
95- btnStart . Enabled = true ;
96- btnSelectFolder . Enabled = true ;
97- txtPath . Enabled = true ;
98- btnStop . Enabled = false ;
99- btnStop . Enabled = true ;
100- }
63+ private void FW_OnDeleted ( object ? sender , FileChangedEvent e )
64+ {
65+ txtConsole . Text += string . Format ( "[del] {0} | {1}" ,
66+ Enum . GetName ( e . ChangeType ) ,
67+ e . FullPath ) + " \r \n " ;
68+ }
10169
70+ private void FW_OnCreated ( object ? sender , FileChangedEvent e )
71+ {
72+ txtConsole . Text += string . Format ( "[cre] {0} | {1}" ,
73+ Enum . GetName ( e . ChangeType ) ,
74+ e . FullPath ) + "\r \n " ;
75+ }
10276
103- private void BtnSelectFolder_Click ( object sender , EventArgs e )
104- {
105- var fb = new FolderBrowserDialog ( ) ;
77+ private void FW_OnRenamed ( object ? sender , FileChangedEvent e )
78+ {
79+ txtConsole . Text += string . Format ( "[ren] {0} | {1} ----> {2}" ,
80+ Enum . GetName ( e . ChangeType ) ,
81+ e . OldFullPath ,
82+ e . FullPath ) + "\r \n " ;
83+ }
10684
85+ private void FW_OnLog ( string value )
86+ {
87+ txtConsole . Text += $@ "[log] { value } " + "\r \n " ;
88+ }
10789
108- if ( fb . ShowDialog ( ) == DialogResult . OK )
109- {
110- txtPath . Text = fb . SelectedPath ;
90+ private void BtnStop_Click ( object sender , EventArgs e )
91+ {
92+ _fw . Stop ( ) ;
93+
94+ btnStart . Enabled = true ;
95+ btnSelectFolder . Enabled = true ;
96+ txtPath . Enabled = true ;
97+ btnStop . Enabled = false ;
98+ btnStop . Enabled = true ;
99+ }
100+
101+
102+ private void BtnSelectFolder_Click ( object sender , EventArgs e )
103+ {
104+ var fb = new FolderBrowserDialog ( ) ;
111105
112- _fw . Stop ( ) ;
113- _fw . Dispose ( ) ;
114- }
115- }
116106
117- private void Form1_FormClosing ( object sender , FormClosingEventArgs e )
107+ if ( fb . ShowDialog ( ) == DialogResult . OK )
118108 {
109+ txtPath . Text = fb . SelectedPath ;
110+
111+ _fw . Stop ( ) ;
119112 _fw . Dispose ( ) ;
120113 }
121114 }
115+
116+ private void Form1_FormClosing ( object sender , FormClosingEventArgs e )
117+ {
118+ _fw . Dispose ( ) ;
119+ }
122120}
0 commit comments