[Mono-list] FileSystemWatcher() problem ..??

Kieren Drapala kieren@tsw.com.au
Fri, 9 Jan 2004 14:37:13 +1000


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C3D66A.40B0C570
Content-Type: text/plain

Hello,

I'm a newbie to mono and I'm struggling with the use of the object
FileSystemWatcher() on Linux version 2.4.23 (gcc version 3.3.2 (Debian))
running mono 0.29 Mono JIT compiler version 0.29, (C) 2002, 2003 Ximian,
Inc.
The code doesn't pick up on any events I have setup.
Should I have FAM daemon installed ?

Heres the code I'm trying to use, and it all compiles ok.


using System;
using System.IO;

public class Watcher
{

   public static void Main()
   {

      try
      {

     
      // Create a new FileSystemWatcher and set its properties.
      FileSystemWatcher watcher = new FileSystemWatcher();
      watcher.Path = @"/home/kieren/BP_IN/"; 

   
      
      /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
      watcher.NotifyFilter =   NotifyFilters.FileName |
NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.DirectoryName;

      watcher.Filter = "";    //"*.*";

      // Add event handlers.
      watcher.Changed += new FileSystemEventHandler(OnChanged);
      watcher.Created += new FileSystemEventHandler(OnChanged);
      watcher.Deleted += new FileSystemEventHandler(OnChanged);
      watcher.Renamed += new RenamedEventHandler(OnRenamed);

      // Begin watching.
      watcher.EnableRaisingEvents = true;

      // Wait for the user to quit the program.
      Console.WriteLine("Press \'q\' to quit the sample.");
      while(Console.Read()!='q');
      }
      catch (Exception ex)
      {
         Console.WriteLine(ex.Message);
      }
   }

   // Define the event handlers.
   public static void OnChanged(object source, FileSystemEventArgs e)
   {
      try
      {
         // Specify what is done when a file is changed, created, or
deleted.
         Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
      }
      catch (Exception ex)
      {
         Console.WriteLine(ex.Message);
      }
   }

   public static void OnRenamed(object source, RenamedEventArgs e)
   {
      try
      {
      // Specify what is done when a file is renamed.
      Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath); //,
e.FullPath);
      }
      catch (Exception ex)
      {
         Console.WriteLine(ex.Message);
      }
   }
}


Please advise, or any reference / example material on FileSystemWatcher I
should be looking at, thanks in advance, regards, 



Kieren Drapala
Analyst / Programmer
The System Works Pty Ltd




------_=_NextPart_001_01C3D66A.40B0C570
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Dus-ascii">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2654.45">
<TITLE>FileSystemWatcher() problem ..??</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">Hello,</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">I'm a newbie to mono and I'm =
struggling with the use of the object FileSystemWatcher() on Linux =
version 2.4.23 (gcc version 3.3.2 (Debian)) running mono 0.29 Mono JIT =
compiler version 0.29, (C) 2002, 2003 Ximian, Inc.</FONT></P>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">The code doesn't pick up on any =
events I have setup.</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">Should I have FAM daemon installed =
?</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">Heres the code I'm trying to use, and =
it all compiles ok.</FONT>
</P>
<BR>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">using System;</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">using System.IO;</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">public class Watcher</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">{</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp; public static void =
Main()</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp; {</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
try</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
{</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // =
Create a new FileSystemWatcher and set its properties.</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
FileSystemWatcher watcher =3D new FileSystemWatcher();</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
watcher.Path =3D @&quot;/home/kieren/BP_IN/&quot;; </FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* =
Watch for changes in LastAccess and LastWrite times, and</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp; the renaming of files or directories. */</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
watcher.NotifyFilter =3D&nbsp;&nbsp; NotifyFilters.FileName | =
NotifyFilters.LastWrite | NotifyFilters.Size | =
NotifyFilters.DirectoryName;&nbsp; </FONT></P>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
watcher.Filter =3D &quot;&quot;;&nbsp;&nbsp;&nbsp; =
//&quot;*.*&quot;;</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Add =
event handlers.</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
watcher.Changed +=3D new FileSystemEventHandler(OnChanged);</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
watcher.Created +=3D new FileSystemEventHandler(OnChanged);</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
watcher.Deleted +=3D new FileSystemEventHandler(OnChanged);</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
watcher.Renamed +=3D new RenamedEventHandler(OnRenamed);</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // =
Begin watching.</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
watcher.EnableRaisingEvents =3D true;</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // =
Wait for the user to quit the program.</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
Console.WriteLine(&quot;Press \'q\' to quit the sample.&quot;);</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
while(Console.Read()!=3D'q');</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
}</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch =
(Exception ex)</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
{</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
Console.WriteLine(ex.Message);</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
}</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp; }</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp; // Define the event =
handlers.</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp; public static void =
OnChanged(object source, FileSystemEventArgs e)</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp; {</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
try</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
{</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // =
Specify what is done when a file is changed, created, or =
deleted.</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
Console.WriteLine(&quot;File: &quot; +&nbsp; e.FullPath + &quot; &quot; =
+ e.ChangeType);</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
}</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch =
(Exception ex)</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
{</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
Console.WriteLine(ex.Message);</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
}</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp; }</FONT>
</P>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp; public static void =
OnRenamed(object source, RenamedEventArgs e)</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp; {</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
try</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
{</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // =
Specify what is done when a file is renamed.</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
Console.WriteLine(&quot;File: {0} renamed to {1}&quot;, e.OldFullPath); =
//, e.FullPath);</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
}</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch =
(Exception ex)</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
{</FONT>
<BR><FONT SIZE=3D2 =
FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
Console.WriteLine(ex.Message);</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
}</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">}</FONT>
</P>
<BR>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">Please advise, or any reference / =
example material on FileSystemWatcher I should be looking at, thanks in =
advance, regards, </FONT></P>
<BR>
<BR>

<P><FONT SIZE=3D2 FACE=3D"Tahoma">Kieren Drapala</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">Analyst / Programmer</FONT>
<BR><FONT SIZE=3D2 FACE=3D"Tahoma">The System Works Pty Ltd</FONT>
</P>
<BR>
<BR>

</BODY>
</HTML>
------_=_NextPart_001_01C3D66A.40B0C570--