I am trying to programatically create and dispose of file system watchers, but I am running into an issue when trying to dispose of them.
When I try to use the code:
// dispose of any existing watchers.
foreach (FileSystemWatcher fileWatcher in this.Controls)
{
fileWatcher.Dispose();
}
I am receiving a System.InvalidCastException exception.
The details show:
> {"Unable to cast object of type 'System.Windows.Forms.Button' to type 'System.IO.FileSystemWatcher'."}
Which is obviously true, but I thought with the qualifier of FileSystemWatcher this would work by skipping over any controls that are not System.IO.FileSystemWatcher. Evidently not.
Any suggestions?
PS for the admins - When posting this I was receiving this error as well:
The code snippet in your post is formatted incorrectly. Please use the Code button in the editor toolbar when posting whitespace-sensitive text or curly braces.
My code snippet was inserted using the Code button...odd...