Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnChanged events with list boxes 1

Status
Not open for further replies.

jwdcfdeveloper

Programmer
Mar 20, 2001
170
US
I am using the FileSystemWatcher class in a Winform to watch and then report changes to a list box like this:


FileSystemWatcher watcher = new FileSystemWatcher(@"C:\");

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{

}

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);

}

private void button1_Click(object sender, System.EventArgs e)
{
folderBrowserDialog1.ShowDialog();

}

private void OnChanged(object sender, FileSystemEventArgs e)
{
listBox1.Items.Add(
DateTime.Now.ToShortDateString() +" "+ e.ChangeType + " " +
e.Name + " " + e.FullPath);


}

private void button2_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
}

private void button4_Click(object sender, System.EventArgs e)
{
Application.Exit();
}


}


I cannot get the data to display in the list box at all after I open the explorer window and select a folder to watch. I did not hook up the code yet for the checkbox that indicates a watched folder, because I'm still trying to get the FileSystemWatcher to do it's job. Does anyone have any ideas about how to get this event to work.
 
1.--> private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
watcher.Changed += new FileSystemEventHandler(OnChanged);
You cannot do that in a SelectedIndexChanged() handler.
2. You should set watcher.NotifyFilter=... and eventually watcher.Filter = "*.*";
3. Add watcher.EnableRaisingEvents = true; to start capturing the events
So, the pattern is the following:
//1. Create a watcher object :
FileSystemWatcher watcher = new FileSystemWatcher();
//2. set the path:
watcher.Path = @"C:\";
//3. Set watcher.NotifyFilter
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
//4. set Filter if needed
watcher.Filter = "*.log";
//5. add events
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
//6. Begin watching
watcher.EnableRaisingEvents = true;

//7. Display the results in a listbox

private static void OnChanged(object source, FileSystemEventArgs e)
{
ListBox1.Items.Add("File: " + e.FullPath + " " + e.ChangeType);
}
[/code]
-obislavu-








 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top