These tasks can be accomplished by using the .NET class FileSystemWatcher.
You can watch for changes in files and subdirectories of the specified
directory. The component can watch files on a local computer, a network
drive, or a remote computer.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace File_system_watcher
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
label1.Text = folderBrowserDialog1.SelectedPath;
fileSystemWatcher1.Path = folderBrowserDialog1.SelectedPath;
fileSystemWatcher1.IncludeSubdirectories = true;
fileSystemWatcher1.Filter = "*.*";
fileSystemWatcher1.EnableRaisingEvents = true;
}
}
private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
{
listBox1.Items.Add(e.Name + "Changed.");
}
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
listBox1.Items.Add(e.Name + "was created.");
}
private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
listBox1.Items.Add(e.Name + "was deleted.");
}
private void watcher_Renamed(object sender, System.IO.RenamedEventArgs e)
{
listBox1.Items.Add(e.Name + "was renamed.");
}
}
}
OUTPUT
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace File_system_watcher
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
label1.Text = folderBrowserDialog1.SelectedPath;
fileSystemWatcher1.Path = folderBrowserDialog1.SelectedPath;
fileSystemWatcher1.IncludeSubdirectories = true;
fileSystemWatcher1.Filter = "*.*";
fileSystemWatcher1.EnableRaisingEvents = true;
}
}
private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
{
listBox1.Items.Add(e.Name + "Changed.");
}
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
listBox1.Items.Add(e.Name + "was created.");
}
private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
listBox1.Items.Add(e.Name + "was deleted.");
}
private void watcher_Renamed(object sender, System.IO.RenamedEventArgs e)
{
listBox1.Items.Add(e.Name + "was renamed.");
}
}
}
OUTPUT