473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

FileSystemWatcher Question

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);
}
Nov 17 '05 #1
3 2687
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.

"Krish" <vi***************@GMAIL.com> wrote in message
news:ug**************@TK2MSFTNGP10.phx.gbl...
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);
}

Nov 17 '05 #2
Thank you for the reply.
Also adding a piece of following helped to catch files created

while( true )

{

ret = watcher.WaitForChanged(WatcherChangeTypes.Created) ;

}

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:u$*************@TK2MSFTNGP10.phx.gbl...
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.

"Krish" <vi***************@GMAIL.com> wrote in message
news:ug**************@TK2MSFTNGP10.phx.gbl...
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);
}


Nov 17 '05 #3
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.

"Krish" <vi***************@GMAIL.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Thank you for the reply.
Also adding a piece of following helped to catch files created

while( true )

{

ret = watcher.WaitForChanged(WatcherChangeTypes.Created) ;

}

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:u$*************@TK2MSFTNGP10.phx.gbl...
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.

"Krish" <vi***************@GMAIL.com> wrote in message
news:ug**************@TK2MSFTNGP10.phx.gbl...
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);
}



Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: David | last post by:
I have been working on trying to write a directory watcher service. One of the requirments is that it be able to watch multiple directories, not sub directories of one parent directory, but just...
20
by: J-T | last post by:
We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business...
1
by: dinny | last post by:
Question about FileSystemWatcher: How can I determine if a delete notification was a file or a directory. I was using the DirectoryInfo.exists method to determine if I am receiving file or...
5
by: =?Utf-8?B?Sm9obiBT?= | last post by:
I am trying to find out if there is a way to tell if there is already a filesystemwatcher (created by a webservice) monitoring a folder. I have a webservice that creates a filesystemwatcher,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.