Does the. Net Monitoring folder have new file generation and confirms that the file is not being consumed by another program?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.