Quote:
The Changed event is raised when changes are made to the size, system attributes, last write time, last access time, or security permissions of a file or directory in the directory being monitored.
(FileSystemWatcher.Changed Event (System.IO) | Microsoft Docs[^])
So files which are opened for writing (or read / write) will not be detected as changed until the file is actually closed (or possibly flushed, though that is less likely)
Try it yourself:
Set up a timer that displays the data:
FileInfo fi = new FileInfo(@"D:\Test Data\AAA.txt");
txtFileInfo.Text = $"{fi.LastAccessTime}|{fi.LastWriteTime}|{fi.CreationTime}";
Then three buttons:
private FileStream fs = null;
private void butOpen_Click(object sender, EventArgs e)
{
fs = new FileStream(@"D:\Test Data\AAA.txt", FileMode.Open, FileAccess.ReadWrite);
}
private void butClose_Click(object sender, EventArgs e)
{
if (fs != null)
{
fs.Close();
fs = null;
}
}
private void butChange_Click(object sender, EventArgs e)
{
if (fs != null)
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToLongTimeString());
fs.Write(data, 0, data.Length);
}
}
The access info isn't altered until the stream is closed - which makes sense, because that's the only point at which the system can be sure that the file data is internally consistent rather than being "half way changed".