TargetInvocationException was unhandled error

May 15 2007 2:38 AM
hi everyone!
   I am tryin to develop a C# windows application which uses a FileSystemWatcher to detect any xml files tat r added to a particular directory and print out the newly added files in a textbox.However, this works only the first time around.This means that the first time i add a new xml file to the folder it prints out into the textbox however when i try adding another new file into the folder the application is not able to print it out in the textbox.my code is below, would be really greateful if anyone could help me out. I am a begginer in C#, so please forgive me if this is a obvious question.Thanks
Savitha

Form1.cs:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Xml;

namespace testing

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)

{

XmlTextReader reader;

textBox1.Text = "new file created at:" + e.FullPath.ToString();

reader = new XmlTextReader(e.FullPath.ToString());

while (reader.Read())

{

textBox1.Text = textBox1.Text + reader.NodeType;

switch (reader.NodeType)

{

case XmlNodeType.Element:

textBox1.Text = textBox1.Text + "\r\n<" + reader.Name + ">";

break;

case XmlNodeType.Text:

textBox1.Text = textBox1.Text + reader.Value;

break;

case XmlNodeType.EndElement:

textBox1.Text = textBox1.Text + "\r\n</" + reader.Name + ">";

break;

}

}

}

private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)

{

}

}

}


Program.cs:

using System;

using System.Collections.Generic;

using System.Windows.Forms;

namespace testing

{

static class Program

{

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

//the error box points out the line below saying tat its a TargetInvocationException
//the inner error is FileNotFoundException
Application
.Run(new Form1());

}

}

}