G
Guest
I am testing the FileSystemWatcher class for possible use in an application I
am going to be writing. I have a simple test application that lets met
browse to a folder and I am looking for a change event.
I have two problems. First, when a file is copied into the folder, four
change events fire. When I edit the file and save it, three change events
fire. I am working with plain text files being edited in NotePad. I really
just want to get one event that tells me when a file has been changed, or
when a new file is copied into the folder.
I am including the relavent code below. I would appreciate any suggestions
anyone has or insights into how the FileSystemWatcher class works.
private void cmdGetFolder_Click(object sender, System.EventArgs e)
{
this.folderBrowserDialog1.ShowDialog(this);
this.txtDirectoryToMonitor.Text = this.folderBrowserDialog1.SelectedPath;
FileSystemWatcher objWatcher = new FileSystemWatcher(
this.folderBrowserDialog1.SelectedPath);
objWatcher.NotifyFilter = NotifyFilters.LastWrite;
objWatcher.Changed += new FileSystemEventHandler(OnChanged);
objWatcher.EnableRaisingEvents = true;
}
private void OnChanged(object source, FileSystemEventArgs e)
{
ListViewItem objItem;
// Add the event to a list view control
// I was ignoring temp files when I tried this with Word documents
// but that is another problem.
if (e.FullPath.EndsWith(".tmp") == false)
{
objItem = new ListViewItem(e.FullPath);
objItem.SubItems.Add(e.ChangeType.ToString());
this.lvEvents.Items.Add(objItem);
}
}
am going to be writing. I have a simple test application that lets met
browse to a folder and I am looking for a change event.
I have two problems. First, when a file is copied into the folder, four
change events fire. When I edit the file and save it, three change events
fire. I am working with plain text files being edited in NotePad. I really
just want to get one event that tells me when a file has been changed, or
when a new file is copied into the folder.
I am including the relavent code below. I would appreciate any suggestions
anyone has or insights into how the FileSystemWatcher class works.
private void cmdGetFolder_Click(object sender, System.EventArgs e)
{
this.folderBrowserDialog1.ShowDialog(this);
this.txtDirectoryToMonitor.Text = this.folderBrowserDialog1.SelectedPath;
FileSystemWatcher objWatcher = new FileSystemWatcher(
this.folderBrowserDialog1.SelectedPath);
objWatcher.NotifyFilter = NotifyFilters.LastWrite;
objWatcher.Changed += new FileSystemEventHandler(OnChanged);
objWatcher.EnableRaisingEvents = true;
}
private void OnChanged(object source, FileSystemEventArgs e)
{
ListViewItem objItem;
// Add the event to a list view control
// I was ignoring temp files when I tried this with Word documents
// but that is another problem.
if (e.FullPath.EndsWith(".tmp") == false)
{
objItem = new ListViewItem(e.FullPath);
objItem.SubItems.Add(e.ChangeType.ToString());
this.lvEvents.Items.Add(objItem);
}
}