Microsoft KB Archive/300605

= How To Monitor Win32 File System Changes in Java =

Article ID: 300605

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Software Development Kit for Java 4.0
 * Microsoft Visual J++ 6.0 Standard Edition

-



This article was previously published under Q300605



SUMMARY
This article describes how you can use the Win32 FindFirstChangeNotification function and its related functions to monitor file system changes from an application.



MORE INFORMATION
In this section, you create a Java application that monitors the file system for file name changes. The Java application code uses the Microsoft Java Delegate mechanism to create a simple event that is named fileEvent. This event is triggered by the underlying file system events. The code creates a Java Console application that creates a thread to monitor the file system. This thread displays messages in the Java Console window when a file change notification event is triggered.  Create a new Console application project named Class1.java in Visual J++ 6.0.  Replace the default Class1 code that Visual J++ generates with the following code: import com.ms.wfc.core.*;

public class Class1 {   public static void main (String[] args) { Class1 c = new Class1; c.start; }   public void start { System.out.println(&quot;Starting Thread&quot;); FileSystemCtrl f = new FileSystemCtrl; f.addOnFileSystemEvent(new EventHandler(this.fileEvent)); f.run; }   public void fileEvent(Object source, Event e) { System.out.println(&quot;FileEvent Handled&quot;); } }

class FileSystemCtrl extends Thread { EventHandler fileEventDelegate = null; public void run { int[] handles = new int[1]; handles[0] = com.ms.win32.Kernel32.FindFirstChangeNotification(&quot;c:\\&quot;,true,com.ms.win32.winf.FILE_NOTIFY_CHANGE_FILE_NAME); while(true) { int waitStatus = com.ms.win32.Kernel32.WaitForMultipleObjects(handles.length,handles,false,com.ms.win32.wini.INFINITE); switch(waitStatus) { case com.ms.win32.winw.WAIT_OBJECT_0: OnFileSystemEvent(new Event); com.ms.win32.Kernel32.FindNextChangeNotification(handles[0]); break; }       }    }    public final void addOnFileSystemEvent(EventHandler handler) { fileEventDelegate = (EventHandler)com.ms.lang.Delegate.combine(fileEventDelegate,handler); }   protected void OnFileSystemEvent(Event event) { if(fileEventDelegate!=null){ fileEventDelegate.invoke(this,event); }   } }                      Save Class1.java. At a command prompt, run the following commands (which use the tools in the Microsoft Software Development Kit for Java) to build and to execute the code:

jvc /nomessage /x- class1.java

start jview class1

 Click Start, and then click Run. Type Notepad C:\Test.txt, and then click OK. When you are prompted to create a new file, click OK. Notice that the following message appears in the Java Console window:

<pre class="fixed_text">FileEvent Handled

</li></ol>

Keywords: kbhowto KB300605

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.