Showing posts with label FileSystemWatcher. Show all posts
Showing posts with label FileSystemWatcher. Show all posts

Tuesday, June 17, 2014

C# - FileSystemWacher Beispiel

Kurzes Beispiel wie die FileSystemWaccher Klasse eingebunden werden kann.

In meinem Beispiel verwende ich eine leere Windows Form Applikation.

Erster Schritt, wir fügen eine TextBox und einen Button zu der Form.












Also nächstes fügen wir die FileSystemWatcher Klasse hinzu.
In diesem Beispiel verwende ich den StartButton um die FileSystemWatcher Klasse zu initialisieren.











Die FileSystemWatcher Klasse soll einen lokalen Pfad überwachen.

Mit watcher.Created += watcherEventwatcher.Deleted += watcherEvent and watcher.Renamed += watcherEvent erstellen wir die Events welche überwacht werden sollen.
Das erste Event dient zur Überwachung für neu erstellte Objekte. (watcher.Created += watcherEvent)
Das zweite Event dient zur Überwachung für gelöschte Objekte. (watcher.Deleted += watcherEvent)
Das dritte Event dient zur Überwachung für umbenannte Objekte. (watcher.Renamed += watcherEvent)

Objekte können in diesem Fall zum Beispiel Ordner oder Dateien sein.

Als nächsten Schritt implementieren wie die Aktion welche hinter dem watcherEvent ausgeführt werden soll.



















Mit dieser Funktion arbeiten wir alle 3 Events ab.
In diesem Beispiel schreibe ich nur den Event-Typ in die TextBox.

Dazu müssen wir das ganze mit einem delegate versehen.
Das ist notwendig, weil der FileSystemWatcher im Hintergrund als eigener Prozess läuft und prozessübergreifende Änderungen an der Form ohne delegate nicht erlaubt sind.

Dazu habe ich eine Funkion "UpdateWatcherOutput" und ein dazugehöriges delegate UpdateWatcherOutputCallback erstellt.

In dieser Funktion können wir Änderungen an der Form zur Laufzeit vornehmen.

Zusätzlich überprüfen wir im FileSystemWatcher ob ein Invoke (Prozessänderung) notwendig ist. (WatcherOutputTextBox.InvokeRequired)

Solle dies zutreffen (TRUE), erstellen wir ein neues delegate UpdateWatcherOutputCallback.


Jetzt können wir das kleine Programm starten und mit dem Button können wir dem FileSystemWatcher mitteilen, dass er mit seiner Aufgabe beginnen soll.
Wenn wir jetzt einen Ordner oder eine Datei in dem angegeben Pfad erstellen, wird Create in der TextBox erscheinen.

Diese Spielerei kann nach Belieben erweitert werden.

Have fun :-)

Monday, June 16, 2014

C# - FileSystemWatcher

Short example how to implement the FileSystemWatcher class.


In my example I´ve used a empty windows forms application.

First stepp, add a textbox and a button to the form.












Next we will implement the FileSystemWatcher class.
I´ve used in this example the Start button to initialize the FileSystemWatcher class.











I´ve created the FileSystemWatcher class to monitor a local path.

With watcher.Created += watcherEventwatcher.Deleted += watcherEvent and watcher.Renamed += watcherEvent we will create three events.
The first event will happen if a new file is created in this folder. (watcher.Created += watcherEvent)
The second event will happen if a file is deleted in this folder. (watcher.Deleted += watcherEvent)
The third event will happen if a file is renamed in this folder. (watcher.Renamed += watcherEvent)


As next step we have to create the watcherEvent function.



















Within this function we will handle all three events.
In my example, I will just write the event name to the text box.

Therefore we have to implement a delegate.
This is necessary because the FileSystemWatcher is running in the background as separate thread and it is not possible to update a object in the form from a new sub thread.

To handle this, I´ve created a function called "UpdateWatcherOutput" and also the delegate UpdateWatcherOutputCallback.

In this function we can update the text box with the event name.

In the event function from the FileSystemWatcher we will check if a Invoke is required. (WatcherOutputTextBox.InvokeRequired)

If YES, we will create a new delegate UpdateWatcherOutputCallback and will invoke it.


Now you can start the application and with the Start button the FileSystemWatcher will start with his work.
When you now create, delete or rename a file in the defined path, you will see the event name in the text box.


Have fun :-)