473,473 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2691
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: 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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.