The holiday is coming soon. Good to kaeson, O (Hangzhou _ Hangzhou) O Haha ~
-------------------------------------------------------
I visited the garden yesterday and found a filesystemwatcher thing. After reading it, I can automatically monitor file changes in the background. I deeply regret that I didn't find it before, I just saved a lot of code ......
I tried to use it when I did not have a lot of work before the festival, but I found that there are still some problems, that is, the event will be triggered again. I will solve it later. It's not long ......
Sticking code is king.
Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. io; namespace filewatcher {public partial class form1: FORM {public form1 () {initializecomponent () ;}string pt; // file path filesystemwatcher watcher = new filesystemwatcher (); private void form1_load (Object sender, eventargs e) {control. success = false; radiobutton1.checked = true; listview lst = new listview ();} private void textbox#mousedoubleclick (Object sender, mouseeventargs e) {folderbrowserdialog Path = new folderbrowserdialog (); Path. showdialog (); this. textbox1.text = path. selectedpath. tostring (); this. textbox1.font = new font (textbox1.font, textbox1.font. style | fontstyle. regular); this. textbox1.textalign = horizontalalignment. left; this. textbox1.forecolor = color. black; Pt = textbox1.text;} private void toolstripbutton#click (Object sender, eventargs e) {If (radiobutton1.checked) Pt = "C :\\"; if (radiobutton2.checked) Pt = "D: \ "; if (radiobutton3.checked) Pt =" e :\\ "; if (radiobutton4.checked) Pt =" f :\\ "; filewatcher (); groupbox1.enabled = false ;} private void toolstripbutton2_click (Object sender, eventargs e) {watcher. enableraisingevents = false; groupbox1.enabled = true;} private void filewatcher () {try {If (flag = 1) {watcher. path = @ pt; watcher. includesubdirectories = true; // Global File monitoring, including subdirectory watcher. enableraisingevents = true; // enable file monitoring watcher. created + = new filesystemeventhandler (oncreat); watcher. deleted + = new filesystemeventhandler (ondelet); watcher. changed + = new filesystemeventhandler (onchange);} else {watcher. enableraisingevents = false; return ;}} catch (exception ex) {return ;}} private void oncreat (Object sender, filesystemeventargs e) {listviewitem li = new listviewitem (datetime. now. tostring ("HH: mm: SS"); Li. subitems. add (getname (E. name. tostring (); Li. subitems. add (E. changetype. tostring (); Li. subitems. add (E. fullpath. tostring (); lst. items. add (LI);} private void ondelet (Object sender, filesystemeventargs e) {listviewitem li = new listviewitem (datetime. now. tostring ("HH: mm: SS"); Li. subitems. add (getname (E. name. tostring (); Li. subitems. add (E. changetype. tostring (); Li. subitems. add (E. fullpath. tostring (); lst. items. add (LI);} private void onchange (Object sender, filesystemeventargs e) {listviewitem li = new listviewitem (datetime. now. tostring ("HH: mm: SS"); Li. subitems. add (getname (E. name. tostring (); Li. subitems. add (E. changetype. tostring (); Li. subitems. add (E. fullpath. tostring (); lst. items. add (LI) ;}// process, get the file name private string getname (string Str) {try {string [] sttarr = Str. split (New char [] {convert. tochar ("\")}); int length = sttarr. length; string filename = sttarr [length-1]; return filename;} catch (exception CC) {MessageBox. show ("invalid file path", "prompt"); Return STR ;}}}}
Run Interface
: A small problem occurs suddenly, that is, the event is triggered repeatedly... Find a solution.
Use filesystemwatcher to monitor Disk Files