Click here to Skip to main content
15,883,778 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
i wanted to use filesystemwatcher class to monitor the delete and other events on drive d:\ of my machine.i have a menustrip control named watcher which on clicking fires up a new windows form which will show all the activities going on drive d:\ of my machine like if a file gets deleted it should show a message in the richtextbox control of this form that a particular file has been deleted.i have already built this application however the problem is that when i debug this application the application closes without displaying out any message in richtextbox control however for the messagebox control it works fine but after i click ok in messagebox control application closes.what i want is that application should wait for some more events to occur.

here's the code for the thing that ivé been trying to work out.
FORM1:
------
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void watcherToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


FORM2
------

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication8
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            start();
        }
        public void start()
        {

            FileSystemWatcher fsw = new FileSystemWatcher();
            fsw.Deleted += new FileSystemEventHandler(ondelete);
            fsw.Path = @"d:\";
            WaitForChangedResult wfc = fsw.WaitForChanged(WatcherChangeTypes.All);
        }
        public void ondelete(object sender, FileSystemEventArgs e)
        {
            richTextBox1.AppendText("file deleted");
            start();
            //MessageBox.Show("file deleted");
        }
    }
}


on clicking the the menustripitem of form1 a new windowsform pops up which has a richtextbox control that should print out the appropriate messages defined in the eventhandler for each event of filesystemwatcher class eg.when delete event occurs it should print message in the richtextbox control that a file has been deleted.this works fine for the messagebox.show() function however when i try print the message in richtextbox control application closes
Posted
Updated 17-Mar-13 0:11am
v4
Comments
Richard MacCutchan 17-Mar-13 4:22am    
Please don't expect people to guess what is going on in your program. How can we even begin to make suggestions without seeing some of your code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900