-2

I Have created a program to watch a notepad file using filewatcher.. whenever the text in the file changes.. I made a .exe program to run... the .exe program also runs properly.. But it runs twice... I searched Stackoverflow and also other websites.. but I couldnt understand where I have went wrong.. Frnds Plz help me in clearing my error.. I know this question has been asked many times.. But since im new to this c# I couldnt understand.. Plz help me frnds..

    public Form1()
    {
        InitializeComponent();
    }

    private void filewatcher()
    {
        path = txtBoxPath.Text;
        FileSystemWatcher fileSystemWatcher1 = new FileSystemWatcher();
        fileSystemWatcher1.Path = path;
        fileSystemWatcher1.Filter = "*.txt";
        fileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite;
        fileSystemWatcher1.Changed += new FileSystemEventHandler(OnChanged);
        fileSystemWatcher1.EnableRaisingEvents = true;
    }

    private void tbtextcopy()
    {
        Clipboard.SetText(File.ReadAllText(path));
        this.textBox1.Text = Clipboard.GetText().ToString();
    }
    private void OnChanged(object sender, FileSystemEventArgs e)
    {

        try
        {
            ProcessStartInfo PSI = new ProcessStartInfo(@"C:\Program Files\Ranjhasoft\Icon Changer\Icon changer.exe");
            Process P = Process.Start(PSI);
            P.WaitForExit();
            P.Close();
        }

        finally
        {
            fileSystemWatcher1.EnableRaisingEvents = false;
        }

    }

    private void BrowserBtn_Click(object sender, EventArgs e)
    {

        FolderBrowserDialog dlg = new FolderBrowserDialog();
        dlg.SelectedPath = this.txtBoxPath.Text;
        DialogResult result = dlg.ShowDialog();

        if (result == DialogResult.OK)
        {
            if (!string.IsNullOrEmpty(dlg.SelectedPath))
            {
                this.txtBoxPath.Text = dlg.SelectedPath;
            }
        }

        if (string.IsNullOrEmpty(this.txtBoxPath.Text))
        {
            this.txtBoxPath.Text = appDataFolder;
        }
        if (!Directory.Exists(path))
        {
            MessageBox.Show("Specified folder does not exist");
        }
    }

    private void txtBoxPath_TextChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(this.txtBoxPath.Text))
        {
            this.filewatcher();
        }
    }

}

}

Kopika
  • 122
  • 15
  • 1
    possible duplicate of [FileSystemWatcher Changed event is raised twice](http://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice) – sstan Aug 18 '15 at 12:18
  • so @sstan should i use this fileSystemWatcher1.EnableRaisingEvents = true; in try command instead of filewatcher() – Kopika Aug 18 '15 at 12:22
  • I have read that article too.. I want to know where "I" have gone wrong... – Kopika Aug 18 '15 at 12:22
  • May I know y I got -2? – Kopika Aug 19 '15 at 06:15

1 Answers1

2

You have the code

fileSystemWatcher1.Changed += new FileSystemEventHandler(OnChanged);

you can be repeatedly adding the same event handler to the event.. so yes, it could do it 20 times..

You only need set it to run the code once, so only add it once. If you stop firing events, the event handler doesnt wipe. So set the eventhandler once, and not each time in your filewatcher method which will repeatedly add the same handler and produce multiple responses.

So change your code here:

public Form1()
{
    InitializeComponent();
    fileSystemWatcher1.Changed += new FileSystemEventHandler(OnChanged);
}

and remove it from the filewatcher method. Now it will run once, when the raiseevents is set true, and not at all when it isnt.

BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • thank u @BugFinder How should i make it run once..? Plz explain it clearly . sorry in advance if im being dumb.. – Kopika Aug 18 '15 at 12:25
  • While I can edit your code, it wouldnt help as someone else reading your question will be left wondering wtf you were on about.. seeing the question and answer will make more sense. – BugFinder Aug 18 '15 at 12:29
  • Thank u.. @BugFinder it didnt run twice.. but will the event run again.. whenever the text changes in notepad? – Kopika Aug 18 '15 at 12:40