File change event
How do I get my program to run a function when a specific file has changed?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Kirtan PatelRank #292 · 8.6K reputationPosted Oct 16, 2009, 11:47 AM EDT
using System.IO;
private void Form1_Load(object sender, EventArgs e)
{
FileSystemWatcher fsw = new FileSystemWatcher(@"E:\", "*.txt");
fsw.EnableRaisingEvents = true;
fsw.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Attributes;
fsw.Changed += new FileSystemEventHandler(fsw_Changed);
}
public void fsw_Changed(Object Sender, FileSystemEventArgs e)
{
if (e.Name == "Test.txt")
{
MessageBox.Show("File Changed !!");
}
}Roei BarRank #1,271 · 1.4K reputationPosted Oct 16, 2009, 11:37 AM EDT
either via a control called FileSystemWatcher
like this
http://www.codeproject.com/KB/vb/AdvancedFileSystemWatcher.aspx
or via a timer that will check on the file on every period of time and update the application on that.