FileSystemWatcher problem

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 1
Reputation: scatterjoy is an unknown quantity at this point 
Solved Threads: 0
scatterjoy scatterjoy is offline Offline
Newbie Poster

FileSystemWatcher problem

 
0
  #1
Aug 9th, 2008
I have a FileSystemWatcher in my code, and the event it's linked to isn't triggering when a file is changed. I watched it in Debug, and the path, notify filter and EnableRaisingEvents are all being set correctly. I've looked at several code examples for FileSystemWatcher and I'm not seeing what I did wrong.

Please help!

  1. public partial class frm_main : Form
  2. {
  3. public frm_main()
  4. {
  5. InitializeComponent();
  6. }
  7.  
  8. public FileSystemWatcher monitor;
  9. public ImageSet myImgSet;
  10.  
  11. /// <summary>
  12. /// called when a folder is selected
  13. /// </summary>
  14. /// <param name="files">A string array of directories</param>
  15. private void DirSelected(string[] dir)
  16. {
  17. try
  18. {
  19. DirectoryInfo dirInfo = new DirectoryInfo(dir[0]);
  20.  
  21. // start monitoring folder
  22. // DirSelected normally does a bunch of other stuff, currently commented out in my code
  23. FileWatcher(dirInfo);
  24. }
  25. catch (ArgumentException ex)
  26. {
  27. MessageBox.Show(ex.Message);
  28. }
  29. }
  30.  
  31. /// <summary>
  32. /// Sets up the file monitoring
  33. /// </summary>
  34. /// <param name="dir">The directory to be monitored</param>
  35. private void FileWatcher(DirectoryInfo dir)
  36. {
  37. // set up file watcher
  38. if (monitor == null)
  39. {
  40. monitor = new FileSystemWatcher();
  41. }
  42.  
  43. monitor.Path = dir.FullName;
  44. monitor.NotifyFilter = NotifyFilters.LastWrite;
  45.  
  46. //Only monitor .bmp files
  47. monitor.Filter = "*.bmp";
  48.  
  49. monitor.Changed += new FileSystemEventHandler(MonitorChanged);
  50.  
  51. // begin watching
  52. monitor.EnableRaisingEvents = true;
  53. }
  54.  
  55. /// Event Handler for FileSystemWatcher
  56. private void MonitorChanged(object s, FileSystemEventArgs e)
  57. {
  58. MessageBox.Show(e.Name);
  59. }
  60. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 39
Reputation: nvmobius is an unknown quantity at this point 
Solved Threads: 4
nvmobius nvmobius is offline Offline
Light Poster

Re: FileSystemWatcher problem

 
0
  #2
Aug 11th, 2008
Code looks fine, might be a NTFS security issue.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: FileSystemWatcher problem

 
0
  #3
Aug 12th, 2008
To be sure its not permissions

use this at the top of filewatcher
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]

also make sure dirinfo is a fully qualified path

and one last thing, why pass in the array to dirselected when you are only trying to poll the first folder
Last edited by dickersonka; Aug 12th, 2008 at 6:34 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC