Guest User

Untitled

a guest
Jan 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. class Program
  2. {
  3. private static void Main(string[] args)
  4. {
  5. FileSystemWatcher fileWatcher = new FileSystemWatcher();
  6. fileWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  7. fileWatcher.Created += fileWatcher_Created;
  8. fileWatcher.EnableRaisingEvents = true;
  9. Console.ReadLine();
  10. }
  11.  
  12. private static void fileWatcher_Created(object sender, FileSystemEventArgs e)
  13. {
  14. WorkOnFile(e.FullPath);
  15. }
  16.  
  17.  
  18. //must be done completely. How do I ensure it?
  19. private static void WorkOnFile(string fileName)
  20. {
  21. using (FileStream f = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
  22. {
  23. Thread.Sleep(40000); // some long operations
  24. }
  25. }
  26. }
  27.  
  28. var scanDirectoryIn = new DirectoryInfo(folderIn);
  29. foreach (var fileInfo in scanDirectoryIn.GetFiles())
  30. {
  31. if (fileInfo.Extension != ".csv") continue;
  32. if (DateTime.UtcNow.Subtract(fileInfo.LastWriteTimeUtc).TotalMinutes < 5) continue;
  33. try
  34. {
  35. fileInfo.MoveTo(folderOut + "\" + fileInfo.Name);
  36. }
  37. catch (Exception) {}
  38. }
  39.  
  40. //must be done completely. How do I ensure it?
  41. private static void WorkOnFile(string fileName)
  42. {
  43. while(true){
  44. try{
  45. using (FileStream f = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
  46. {
  47. Thread.Sleep(40000); // some long operations
  48.  
  49. break; //exit while() infinite loop
  50. }
  51. }
  52. catch(Exception e){
  53. //file is locked because being written. wait a few seconds then retry
  54. Thread.Sleep(10000);
  55. }
  56. }
  57. }
Add Comment
Please, Sign In to add comment