C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Changed: This is triggered when a file in the directory is changed. It may execute twice per edit.
Note: To prevent duplicate work, it is a good idea to use a flag bool to indicate what happened—so we just set "needs update" to true.
C# program that uses FileSystemWatcher
using System;
using System.IO;
class Program
{
static void Main()
{
Console.WriteLine("MAIN");
Init();
// Run an infinite loop.
while (true)
{
Console.WriteLine("TYPE SOMETHING");
string line = Console.ReadLine();
Console.WriteLine("TYPED: " + line);
}
}
/// <summary>
/// Watcher.
/// </summary>
static FileSystemWatcher _watcher;
/// <summary>
/// Init.
/// </summary>
static void Init()
{
Console.WriteLine("INIT");
string directory = @"C:\programs\";
Program._watcher = new FileSystemWatcher(directory);
Program._watcher.Changed +=
new FileSystemEventHandler(Program._watcher_Changed);
Program._watcher.EnableRaisingEvents = true;
Program._watcher.IncludeSubdirectories = true;
}
/// <summary>
/// Handler.
/// </summary>
static void _watcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("CHANGED, NAME: " + e.Name);
Console.WriteLine("CHANGED, FULLPATH: " + e.FullPath);
// Can change program state (set invalid state) in this method.
// ... Better to use insensitive compares for file names.
}
}
Output
MAIN
INIT
TYPE SOMETHING
test
TYPED: test
TYPE SOMETHING
CHANGED, NAME: file.txt
CHANGED, FULLPATH: C:\programs\file.txt
CHANGED, NAME: file.txt
CHANGED, FULLPATH: C:\programs\file.txt
hello
TYPED: hello
TYPE SOMETHING
CHANGED, NAME: file.txt
CHANGED, FULLPATH: C:\programs\file.txt
CHANGED, NAME: file.txt
CHANGED, FULLPATH: C:\programs\file.txt
Contents, file.txt, part 1:
HELLO WORLD!
Contents, file.txt, part 2:
HELLO WORLD!
X2
Contents, file.txt, part 3:
HELLO WORLD!
X2
X3
Then: In the bottom tray, select Properties on the fileSystemWatcher1 label. You can specify the directory you want to monitor for changes.
Example: We use a directory named C:\watch. Next in the Properties panel, click on the lightning bolt and add 4 event handlers.
Info: Once the program is run, you can change files in the C:\watch directory and dialog boxes will display the events.
MessageBoxC# program that uses FileSystemWatcher, Windows Forms
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
{
// Occurs when the contents of the file change.
MessageBox.Show(string.Format("Changed: {0} {1}", e.FullPath, e.ChangeType));
}
void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
{
// FullPath is the new file's path.
MessageBox.Show(string.Format("Created: {0} {1}", e.FullPath, e.ChangeType));
}
void fileSystemWatcher1_Deleted(object sender, FileSystemEventArgs e)
{
// FullPath is the location of where the file used to be.
MessageBox.Show(string.Format("Deleted: {0} {1}", e.FullPath, e.ChangeType));
}
void fileSystemWatcher1_Renamed(object sender, RenamedEventArgs e)
{
// FullPath is the new file name.
MessageBox.Show(string.Format("Renamed: {0} {1}", e.FullPath, e.ChangeType));
}
}
}
Note: You may need special code to filter Changed events. This depends on your application.
And: In this way, you could simply scan a directory every five seconds for changes.
Also: You can then call methods based on the changed detected. You could use the FileInfo type as well as the Timer type for this.
TimerFileInfoFileInfo Length