943,712 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 3797
  • C# RSS
Aug 9th, 2008
0

FileSystemWatcher problem

Expand Post »
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!

c# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
scatterjoy is offline Offline
1 posts
since Aug 2008
Aug 11th, 2008
0

Re: FileSystemWatcher problem

Code looks fine, might be a NTFS security issue.
Reputation Points: 11
Solved Threads: 4
Light Poster
nvmobius is offline Offline
39 posts
since Jul 2008
Aug 12th, 2008
0

Re: FileSystemWatcher problem

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.
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Forms App Behaving Oddly Based On User
Next Thread in C# Forum Timeline: Session in Windows Application?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC