Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET Framework
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:
------
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
------
 
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 16-Mar-13 23:03pm
rtz87865
Edited 17-Mar-13 1:11am
(no name)63.4K
v4
Comments
Richard MacCutchan at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 17 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100