# FileSystemWatcher or FileWatcher

**URL:** <https://forums.thehomestack.io/t/filesystemwatcher-or-filewatcher/283>\
**Category:** vb.Net\
**Created:** [September 15, 2007, 1:38pm UTC](https://forums.thehomestack.io/t/filesystemwatcher-or-filewatcher/283 "2007-09-15T13:38:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://forums.thehomestack.io/uploads/default/original/1X/161426793bc0d8bdf0d1a6692b3e44446861cc72.jpeg) [@system](https://forums.thehomestack.io/u/system)\
**Post date:** [September 15, 2007, 1:38pm UTC](https://forums.thehomestack.io/t/filesystemwatcher-or-filewatcher/283/1 "2007-09-15T13:38:55Z")

</div>

Here is a snippet of code for a filewatcher system

```auto

Private fsw As New FileSystemWatcher 

Private Sub btnButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnButton1.Click 
  If Me.btnButton1.Text.ToLower = "watch" Then 
      AddHandler fsw.Created, AddressOf WatchHandler 
      AddHandler fsw.Changed, AddressOf WatchNameChange 
      Watch() 
      Me.btnButton1.Text = "Stop" 
  Else 
      StopWatch() 
      RemoveHandler fsw.Created, AddressOf WatchHandler 
      RemoveHandler fsw.Changed, AddressOf WatchNameChange 
      Me.btnButton1.Text = "Watch" 
  End If 
End Sub 

Private Sub Watch() 
  With fsw 
      .NotifyFilter = NotifyFilters.FileName 
      .Path = "\\yourFileServer\YourPath\" 
      .Filter = "*.txt" 
      .SynchronizingObject = Me 
      .EnableRaisingEvents = True 
  End With 
End Sub 

Private Sub StopWatch() 
  With fsw 
      .EnableRaisingEvents = False 
  End With 
End Sub 

Private Sub WatchHandler(ByVal sender As Object, ByVal e As FileSystemEventArgs) 
  Me.rtbMain.Text &= e.ChangeType & ":" & vbTab & e.FullPath & vbCrLf 
End Sub 

Private Sub WatchNameChange(ByVal sender As Object, ByVal e As FileSystemEventArgs) 
  Me.rtbMain.Text &= e.ChangeType & ":" & vbTab & e.FullPath & vbCrLf 
End Sub

```
