Just as an FYI, here's my code for something similar:
private void btnStartMonitoring_Click(object sender, EventArgs e)
{
System.IO.FileSystemWatcher fsw = new
FileSystemWatcher(txtDirectoryName.Text.ToString() );
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite |
NotifyFilters.FileName | NotifyFilters.DirectoryName;
fsw.Filter = "*.cnk";
fsw.Created += new FileSystemEventHandler(OnCreated);
// Begin watching.
fsw.EnableRaisingEvents = true;
}
private static void OnCreated(object source, FileSystemEventArgs e)
{
string sMessage = "File: " + e.FullPath + " " + e.ChangeType;
MessageBox.Show(sMessage);
}
There is no loop monitoring the status; it simply fires when the event
occurs. Now one thing I'm still fighting with is that I need to Invoke the
process to happen afterwards, otherwise the UI does goofy things. See my
post from 04/08/06. But that's just tweaking... :) Famous last words.
Clint
"Frank" <a-******@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
I figured it out. It seemes that the while loop is the fault. I changed the
while(true) to use Console.ReadLine() which helped.
using System.IO;
namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static bool bNew = false;
static void Main(string[] args)
{
FileSystemWatcher buildWatcher = new FileSystemWatcher();
buildWatcher.Path = "C:\\";
buildWatcher.NotifyFilter = NotifyFilters.LastWrite
| NotifyFilters.FileName;
buildWatcher.Filter = "myfile.txt";
buildWatcher.Changed += new FileSystemEventHandler(OnChanged);
while (true)
{
if (bNew)
{
//. something to do here
}
}
}
public static void OnChanged(object source, FileSystemEventArgs e)
{
bNew = true;
}
}
}
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Frank <a-******@microsoft.com> wrote: I am using FileSystemWatcher to track the change in a text file.
Whenever I
run the program I get 99% CPU utilization. The code doesn't do anything
just
sits and waits in a while loop until the event is fired. Any ideas?
Thanks
Could you post a short but complete program which demonstrates the
problem?
See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too