How can I do this with FileSystemWatcher

R

R.A.

Hi

I have th following code:

class MyClass
{
private string filename;
public int SetInfo (string FileName)
{
filename = FileName;
}

public MyClass ()
{
FileWatcher = new FileSystemWatcher ();
FileWatcher.NotifyFilter = NotifyFilters.FileName;

FileWatcher.Filter = "*.*";

FileWatcher.Path = "C:\\temp";

FileWatcher.Created += new FileSystemEventHandler (OnFileCreated);

}
private static void OnFileCreated(object source, FileSystemEventArgs e)
{

//How to call SetInfo ?




}

}

How can I call SetInfo without having to change it to a static function?
What are the options?


Thanks
 
?

=?iso-8859-1?Q?Andreas_H=E5kansson?=

R.A,

You can call the method from where you are.

private static void OnFileCreated(object source, FileSystemEventArgs e)
{
this.SetInfo(e.Name);
}

Please note that the "this" keyword is optional.

HTH,

//Andreas



--
ANDREAS HÅKANSSON
STUDENT OF SOFTWARE ENGINEERING
andreas (at) selfinflicted.org
Hi

I have th following code:

class MyClass
{
private string filename;
public int SetInfo (string FileName)
{
filename = FileName;
}

public MyClass ()
{
FileWatcher = new FileSystemWatcher ();
FileWatcher.NotifyFilter = NotifyFilters.FileName;

FileWatcher.Filter = "*.*";

FileWatcher.Path = "C:\\temp";

FileWatcher.Created += new FileSystemEventHandler (OnFileCreated);

}
private static void OnFileCreated(object source, FileSystemEventArgs e)
{

//How to call SetInfo ?




}

}

How can I call SetInfo without having to change it to a static function?
What are the options?


Thanks
 
I

Ignacio Machin \( .NET/ C# MVP \)

HI RA,

First of all SetInfo will not compile cause you are not returning an
integer.


You cannot call it unless you make it static, or if you make the watcher
handler not static.

The only way that you dont have to change the declarations is if you
include a reference to the class itself as a static member:
class MyClass
{
static MyClass reference;

....
}
and then in the constructor you assign it:

public MyClass ()
{
reference = this;
}

then you can call the instance method using reference.SetInfo( ... )

Nevertheless, I think that you should not do this, if SetInfo is independend
of the instance you should make it a static member.


Cheers,
 
R

R.A.

It doesn't let me because OnFileCreated is a static function!
"Andreas Håkansson" <andreas (at) selfinflicted.org> wrote in message R.A,

You can call the method from where you are.

private static void OnFileCreated(object source, FileSystemEventArgs e)
{
this.SetInfo(e.Name);
}

Please note that the "this" keyword is optional.

HTH,

//Andreas



--
ANDREAS HÅKANSSON
STUDENT OF SOFTWARE ENGINEERING
andreas (at) selfinflicted.org
Hi

I have th following code:

class MyClass
{
private string filename;
public int SetInfo (string FileName)
{
filename = FileName;
}

public MyClass ()
{
FileWatcher = new FileSystemWatcher ();
FileWatcher.NotifyFilter = NotifyFilters.FileName;

FileWatcher.Filter = "*.*";

FileWatcher.Path = "C:\\temp";

FileWatcher.Created += new FileSystemEventHandler (OnFileCreated);

}
private static void OnFileCreated(object source, FileSystemEventArgs e)
{

//How to call SetInfo ?




}

}

How can I call SetInfo without having to change it to a static function?
What are the options?


Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top