Filesystemwatcher triggers multiple events for one file
I'm using a FileSyswtemWatcher and when a new file is created or
changed, it shows up as a duplicated event. If printing to a PDF file
in the monitored folder, then 4 events come up...the problem I'm having
is mainly with insertions to am SQL database are also duplicated. has
anyone figured out how to supress the additional messages down to just
one event?
JosePosted Feb 9, 2010, 10:08 AM
// PDFfileWatcher
//
this.PDFfileWatcher.EnableRaisingEvents = true;
this.PDFfileWatcher.IncludeSubdirectories = true;
this.PDFfileWatcher.SynchronizingObject = this;
this.PDFfileWatcher.Changed += new System.IO.FileSystemEventHandler(this.PDFfileWatcher_Created);
private void PDFfileWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
string PdfChangeType = e.ChangeType.ToString();
MessageBox.Show("Something Created: " + e.Name.ToString() + " " + e.ChangeType);
}
private void PrintTextFileHandler(object sender, PrintPageEventArgs ppeArgs)
{
//Get the Graphics object
Graphics g = ppeArgs.Graphics;
float linesPerPage = 0;
float yPos = 0;
int count = 0;
//Read margins from PrintPageEventArgs
float leftMargin = ppeArgs.MarginBounds.Left;
float topMargin = ppeArgs.MarginBounds.Top;
string line = null;
//Calculate the lines per page on the basis of the height of the page and the height of the font
linesPerPage = ppeArgs.MarginBounds.Height /
CourierNew11Font.GetHeight(g);
//Now read lines one by one, using StreamReader
while (count < linesPerPage &&
((line = reader.ReadLine()) != null))
{
//Calculate the starting position
yPos = topMargin + (count *
CourierNew11Font.GetHeight(g));
//Draw text
g.DrawString(line, CourierNew11Font, Brushes.Black,
leftMargin, yPos, new StringFormat());
//Move to next line
count++;
}
//If PrintPageEventArgs has more pages to print
if (line != null)
{
ppeArgs.HasMorePages = true;
}
else
{
ppeArgs.HasMorePages = false;
}
}
private void Print_Click(object sender, EventArgs e)
{
// Start PDFfileWatcher
ToBePaidPath = textboxToBePaidPath.Text;
PDFfileWatcher.Path = ToBePaidPath;
PDFfileWatcher.Filter = "*.*";
PDFfileWatcher.IncludeSubdirectories = false;
//Start PDFfileWatcher
if (PDFfileWatcher.EnableRaisingEvents == false)
{
try
{
//Begin monitoring.
PDFfileWatcher.EnableRaisingEvents = true;
MessageBox.Show("PDFfileWatcher is active");
}
catch (IOException ex)
{
MessageBox.Show("There was an error starting the PDF file monitor...Error was: " + ex.Message);
}
}
string fileContent = HiddentextBox1.Text.ToString();
//Create a StreamReader object
try
{
reader = new StreamReader(fileContent);
//Create a CourierNew font with size 11
CourierNew11Font = new Font("Courier New", (11), FontStyle.Bold);
//Create a PrintDocument object
PrintDocument pd = new PrintDocument();
// Set Margins width
Margins margins = new Margins(5, 5, 30, 25);
pd.DefaultPageSettings.Margins = margins;
//Add PrintPage event handler
pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler);
//Call Print Method
pd.Print();
}
catch (IOException ex)
{
MessageBox.Show("Error was: " + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
//Close the reader
if (reader != null)
reader.Close();
}
}
Note, that when the print function runs, I have it set up to print to the default printer, in which my case is "Adobe PDF". A dialog appears which is triggered by save PDF printer where I can name the PDF file as I choose. It is at this stage that the file watcher detects about 4 created events. I'm assuming that the watcher is picking up some sort of temporary files, however the messagebox echoes the name of the same PDF file created 4 times. If I can filter the routine to only echo once, that would be great.
If I change the event to only detect "changed" events, then only one notification comes up. However I need to be able to detect "Created" PDF files, and PDF files that have been modified afterwards with the same watcher if possible, so that either one of the 2 events will do a different action that I choose. (eg INSERT on created, and UPDATE on Changed events to the SQL database).
Kirtan PatelPosted Feb 8, 2010, 8:52 PM