Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > c# > november 2009 >

c# : FileSystemWatcher Question


Krish
10/21/2005 12:00:00 AM
Thank you for the reply.
Also adding a piece of following helped to catch files created

while( true )

{

ret = watcher.WaitForChanged(WatcherChangeTypes.Created);

}



[quoted text, click to view]

Krish
10/21/2005 10:16:40 AM
Hello Gurus,

Pardon me for asking dumb question...

I wrote small csharp program to watch a folder for any created text file...
When i try to run through debugger and stop after "
watcher.EnableRaisingEvents = true;", i manually drag and drop windows text
file and this program doesnt seem to fire up... see code below and pl.
advise what wrong iam doing. Thanks.

public static void Main ()
{
Run();
}

public static void Run()
{
string FilePath = "D:\\BECSolution\\Jobs\\Faster\\bin\\Debug";
Console.WriteLine("File Monitoring Started");
Console.WriteLine("\t{0} is being monitored", FilePath);
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = FilePath;
watcher.NotifyFilter = NotifyFilters.FileName |
NotifyFilters.CreationTime;
watcher.Filter = "*.txt";
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}

private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}

Kevin Spencer
10/21/2005 12:17:41 PM
You're creating your FileSystemWatcher class in function scope. When the
function exits, the FileSystemWatcher is no longer in scope; it is ready for
Garbage Collection. You need to declare it as a field in the class, and set
it up in the method. That way it will last for the lifetime of the class,
not the method.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

[quoted text, click to view]

Kevin Spencer
10/21/2005 2:51:09 PM
The problem with that method is that the method never exits. Unless you have
some other code in there that allows for an exit, the method will never
exit, and unless it is running in a separate thread, it will block all other
method calls, or any other work the class may want to do.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

[quoted text, click to view]

sathish
11/20/2009 2:04:27 AM
public static void Main ()
{
Run();

//Just add this below line...
Console.Read();

}


From http://www.developmentnow.com/g/36_2005_10_0_0_621669/FileSystemWatcher-Question.htm

Posted via DevelopmentNow.com Groups
AddThis Social Bookmark Button