Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Public Class Blah
- Public Sub New()
- InitializeComponent()
- Dim watcher As New FileSystemWatcher("C:")
- watcher.EnableRaisingEvents = True
- AddHandler watcher.Changed, AddressOf watcher_Changed
- End Sub
- Private Sub watcher_Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs)
- MsgBox(e.FullPath)
- End Sub
- End Class
- if( e.ChangeType == WatcherChangeTypes.Changed )
- {
- // There is a nasty bug in the FileSystemWatch which causes the
- // events of the FileSystemWatcher to be called twice.
- // There are a lot of resources about this to be found on the Internet,
- // but there are no real solutions.
- // Therefore, this workaround is necessary:
- // If the last time that the event has been raised is only a few msec away,
- // we ignore it.
- if( DateTime.Now.Subtract (_lastTimeFileWatcherEventRaised).TotalMilliseconds < 500 )
- {
- return;
- }
- _lastTimeFileWatcherEventRaised = DateTime.Now;
- .. handle event
- private Dictionary<String, FileSystemEventArgs> xmlFileChangedEvents = new Dictionary<string, FileSystemEventArgs>();
- private void debugXmlWatcher_Changed(object sender, FileSystemEventArgs e)
- {
- if (!xmlFileChangedEvents.ContainsKey(e.Name))
- xmlFileChangedEvents.Add(e.Name, e);
- xmlChangeTimer.Stop();//Reset the Forms.Timer so that it times out in 100 ms
- xmlChangeTimer.Start();
- }
- private void xmlChangeTimer_Tick(object sender, EventArgs e)
- {
- foreach (FileSystemEventArgs eventArg in xmlFileChangedEvents.Values)
- {
- //
- //Handle the file changed event here
- //
- }
- xmlFileChangedEvents.Clear();
- }
- using System;
- using System.IO;
- using System.Timers;
- namespace Demo
- {
- class FileWatcher
- {
- private FileSystemWatcher watcher = new FileSystemWatcher();
- private Timer t = new Timer();
- public event EventHandler FileChanged;
- public FileWatcher()
- {
- t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
- t.Interval = 1000;
- }
- public void Start(String path)
- {
- watcher.Path = Path.GetDirectoryName(path);
- watcher.Filter = Path.GetFileName(path);
- watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.CreationTime;
- watcher.EnableRaisingEvents = true;
- watcher.Changed += new FileSystemEventHandler(watcher_Changed);
- }
- void watcher_Changed(object sender, FileSystemEventArgs e)
- {
- if (!t.Enabled)
- t.Start();
- }
- void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
- {
- t.Stop();
- if (FileChanged != null)
- FileChanged(this, null);
- }
- }
- }
- FileWatcher FileWatcher1 = new FileWatcher();
- FileWatcher1.FileChanged += new EventHandler(FileWatcher1_FileChanged);
- FileWatcher1.Start("c:test.txt");
- Imports System.IO
- Imports System.Threading
- Public Class Form1
- Private Const MILLISECONDS_TO_WAIT As Integer = 1000
- Private fw As FileSystemWatcher
- Private Shared AccessEntries As List(Of String)
- Private Delegate Sub UpdateBoxDelegate(ByVal msg As String)
- Private Sub UpdateBox(ByVal msg As String)
- If tbMonitor.InvokeRequired Then
- Invoke(New UpdateBoxDelegate(AddressOf UpdateBox), New Object() {msg})
- Else
- tbMonitor.AppendText(msg + vbCrLf)
- End If
- End Sub
- Private Sub AccessEntryRemovalTimer(ByVal RawFileName As Object)
- UpdateBox("Sleeping to watch for " + RawFileName.ToString + " on thread ID " + Thread.CurrentThread.ManagedThreadId.ToString)
- Thread.Sleep(MILLISECONDS_TO_WAIT)
- AccessEntries.Remove(RawFileName.ToString)
- UpdateBox("Removed " + RawFileName.ToString + " in thread ID " + Thread.CurrentThread.ManagedThreadId.ToString)
- End Sub
- Private Sub Changed(ByVal source As Object, ByVal e As FileSystemEventArgs)
- If AccessEntries.Contains(e.Name) Then
- UpdateBox("Ignoring a " + e.ChangeType.ToString + " notification for " + e.Name + " in thread ID " + Thread.CurrentThread.ManagedThreadId.ToString)
- Return
- End If
- Dim AccessTimerThread As Thread
- AccessEntries.Add(e.Name)
- UpdateBox("Adding " + e.Name + " to the collection and starting the watch thread.")
- AccessTimerThread = New Thread(AddressOf AccessEntryRemovalTimer)
- AccessTimerThread.IsBackground = True
- AccessTimerThread.Start(e.Name)
- End Sub
- Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- tbMonitor.ScrollBars = ScrollBars.Both
- AccessEntries = New List(Of String)
- fw = New FileSystemWatcher
- fw.Path = "C:temp"
- fw.NotifyFilter = NotifyFilters.LastWrite Or NotifyFilters.LastAccess Or NotifyFilters.FileName
- AddHandler fw.Changed, AddressOf Changed
- AddHandler fw.Created, AddressOf Changed
- AddHandler fw.Renamed, AddressOf Changed
- fw.EnableRaisingEvents = True
- End Sub
- Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
- fw.EnableRaisingEvents = False
- RemoveHandler fw.Changed, AddressOf Changed
- RemoveHandler fw.Created, AddressOf Changed
- RemoveHandler fw.Renamed, AddressOf Changed
- fw.Dispose()
- End Sub
- public class FileChangeMonitor
- {
- private FileSystemWatcher _fsw;
- DateTime _lastEventTime;
- public event FileSystemEventHandler Changed;
- public FileChangeMonitor(string path, string filter)
- {
- _fsw = new FileSystemWatcher(path, filter);
- _fsw.Changed += new FileSystemEventHandler(_fsw_Changed);
- _fsw.EnableRaisingEvents = true;
- _fsw.NotifyFilter = NotifyFilters.LastWrite;
- _fsw.IncludeSubdirectories = false;
- }
- private void _fsw_Changed(object sender, FileSystemEventArgs e)
- {
- // Fix the FindFirstChangeNotification() double-call bug
- if (DateTime.Now.Subtract(_lastEventTime).TotalMilliseconds > 100)
- {
- _lastEventTime = DateTime.Now;
- if (this.Changed != null)
- this.Changed(sender, e); // Bubble the event
- }
- }
- }
- FileChangeMonitor fcm = new FileChangeMonitor(path, filter);
- fsm.Changed += new FileSystemEventHandler(fsm_Changed);
- ...
- // Class level variable
- bool m_FileSystemWatcherIsMessy = true;
- // inside call back
- if (m_FileSystemWatcherIsMessy) {
- m_FileSystemWatcherIsMessy = false;
- return;
- } else {
- m_FileSystemWatcherIsMessy = true;
- }
Add Comment
Please, Sign In to add comment