Monitoring folder Test program:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.IO;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Threading;7 usingSystem.Threading.Tasks;8 9 namespacefilesystemwatchertestTen { One class Program A { - Static voidMain (string[] args) - { theFileSystemWatcher Watcher =NewFileSystemWatcher ("C:\\filesystemwatcher","*.txt"); -Watcher. NotifyFilter =Notifyfilters.filename; -Watcher. Created + =NewFileSystemEventHandler (filecreated); -Watcher. EnableRaisingEvents =true; + - console.readline (); + } A at Private Static voidFilecreated (Objectsender, FileSystemEventArgs e) - { - if(!file.exists (e.fullpath)) - { - return; - } inConsole.WriteLine ("Created: {0:HH:MM:SS}", DateTime.Now); - to while(!Isfileready (e.fullpath)) + { -Console.WriteLine ("used: {0:HH:MM:SS}", DateTime.Now); the } * //file processing here ... $Console.WriteLine ("Ready : {0:HH:MM:SS}", DateTime.Now);Panax Notoginseng -Thread.Sleep ( +*5); theFileInfo fs =NewFileInfo (e.fullpath); + A varMovetopath =@"\\testServer\Shares\"+FS. Name; the FS. MoveTo (Movetopath); + - if(!file.exists (movetopath)) $ { $Console.WriteLine ("Move faild: {0:HH:MM:SS}", DateTime.Now); - } -Console.WriteLine ("Move Success: {0:HH:MM:SS}", DateTime.Now); the } - Wuyi Static BOOLIsfileready (stringfilename) the { -FileInfo fi =NewFileInfo (filename); WuFileStream fs =NULL; - Try About { $FS =fi. Open (FileMode.Open, fileaccess.readwrite,fileshare.none); - return true; - } - A Catch(IOException) + { the return false; - } $ the finally the { the if(FS! =NULL) the FS. Close (); - } in } the } the}
View Code
File Generation Test Program:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.IO;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Threading;7 usingSystem.Threading.Tasks;8 9 namespacegeneratefiletestTen { One class Program A { - Static voidMain (string[] args) - { theFileStream fs =NewFileStream ("C:\\filesystemwatcher\\test.txt", FileMode.Create); -StreamWriter SW =NewStreamWriter (FS); - - varCurrdatetime =DateTime.Now; + varMaxdatetime = Currdatetime.addseconds (Ten); - +Console.WriteLine ("Log begin: {0}", DateTime.Now.ToString ("HH:mm:ss")); A while(Currdatetime <maxdatetime) at { - SW. WriteLine (DateTime.Now.ToString ()); -Currdatetime =DateTime.Now; - } - - //emptying buffers in SW. Flush (); - //Close the stream to SW. Close (); + FS. Close (); - theConsole.WriteLine ("Log end: {0}", DateTime.Now.ToString ("HH:mm:ss")); * console.readline (); $ }Panax Notoginseng } -}
View Code
Operation Result:
Does the. Net Monitoring folder have new file generation and confirms that the file is not being consumed by another program?