Your FileSystemWatcher is scoped to the method in which you instantiate
it. That means that it goes out of scope and is not available as soon as
the method returns. It may hang around for a bit since you have no code to
dispose it at any point, but you need to bone up on scope and state.
Here's a primer:
Scope is the "area of influence" of a variable. In a class, you have
global and local variables. Global variables are declared outside of any
methods, and are therefore "visible" to all members of the class. Local
variables are declared inside a method, and are therefore "visible" only
to other variables within a single instance (call) of the method. Once the
method returns, it is removed from the stack, and everything in it becomes
available for garbage collection.
Note that this differs from public, private, etc. These are also
definitions of scope, but are more related to encapsulation. Public
members of a class are "visible" from other classes. Private members are
not. Etc.
State refers to the persistence of an object (class instance or primitive)
in memory. Whenever an object is instantiated (created), it must reside in
memory somewhere. The lifetime of an object is limited to the lifetime of
the container in which it resides. A method is instantiated, just like a
variable, whenever you call it. And just like a variable, when it returns,
it is available for garbage collection. The only way to persist an
instance is to put it into something else that persists. Hence, ASP.Net,
which operates using HTTP, which is stateless, has mechanisms for
maintaining state, such as Session, Application, and ViewState.
The global.asax class, as I mentioned in my earlier reply, is a persistent
class, but it's methods are not. They are instantiated like any other
methods, and pass out of scope as soon as they return.
I hope you will study what I've said, and continue to study how OOP works
as you go. In the meantime, you need to understand how to persist your
FileSystemWatcher, as well as making sure that when you are finished with
it, you dispose it.
--
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
J-T said:
Here is what I have done .I have created a class and I instanciate that
class in Application_Start method of my Global.asax ere is the code:
********Gloabl.asax:
//Sets up a wacher on an specific shared folder to pickup zip files
IFPWatcherComponent ifpWacher=new
IFPWatcherComponent(ConfigurationSettings.AppSettings[ "MonitorPath"]);
********MyClass:
/// </summary>
public class IFPWatcherComponent:BusinessObject
{
string PathToMonitor=null;
public IFPWatcherComponent(string PathToMonitor)
{
try
{
this.PathToMonitor = PathToMonitor;
//Check to see if Web.config has appropriate settings
if (this.PathToMonitor.Trim().Length==0 ) return;
// Make a reference to a directory.
DirectoryInfo di = new DirectoryInfo(this.PathToMonitor);
// Create the directory only if it does not already exist.
if (di.Exists == false)
di.Create();
// create an instance of FileSystemWatcher object and assign path to
monitor
FileSystemWatcher watcher = new FileSystemWatcher();
// set necessary filters
watcher.Path = this.PathToMonitor;
//Watch for changes in FileName
watcher.NotifyFilter = NotifyFilters.FileName;
// watch zip files
watcher.Filter = "*.zip";
//Add appropriate event handler
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.EnableRaisingEvents = true;
}
catch
{
throw;
}
}
private void OnCreated(object source, FileSystemEventArgs e)
{
FileStream oImg=null;
BinaryReader oBinaryReader=null;
Uploader uploader=new Uploader();
uploader.ComeFrom="88";
uploader.Comments="comment";
uploader.CreatedByUser="test";
uploader.FileName=e.Name;
try
{
oImg = new FileStream (e.FullPath,FileMode.Open,FileAccess.Read);
oBinaryReader = new BinaryReader(oImg);
byte[] oImgByteArray = oBinaryReader.ReadBytes((int) oImg.Length);
uploader.FileBody=oImgByteArray;
uploader.RecordType=Business.IFP.RecordType.IFP_UPLOAD;
uploader.Upload();
uploader=null;
}
catch(Exception ee)
{
*******************I don't know how to handle the exception
here??***********************
}
finally
{
if (oBinaryReader!=null) oBinaryReader.Close();
if (oImg!=null) oImg.Close();
}
}
Are you sure you are on the right track? A FileSystemWatcher object has
to
exist somewhere to be able to watch. An asp.net application doesn't
exist
anywhere but between a client http request and the server response. A
very
short time. Do you expect the watcher to catch something only when the
server is busy serving client requests?
Eliyahu
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 logic layer(so far so good
and
easy).I initialize my class which is using a FileSystemWatcher in my
Global.asax and everything works fine.I have found FileSystemWatcher
class
not very reliable and sometimes it behavies unexpectedly.I'm afriad
that
it
brings down the whole application.Is there a better way of doing this
**Inside Asp.Net application**.I can't use antother application like
windows
service or schedault taks.Everything needs to be done is ASP.NET
application.
Thanks a lot