I have a file on a network drive that is frequently used by many in our environment. How can I use Java to track how many times the file has been opened/run. I did some research into file watchers http://docs.oracle.com/javase/tutorial/essential/io/notification.html however these seem only to apply to creation, deletion, modification, and overflow. Before I go off on a file watching tangent, is this possible and what is the best way to do it?
Asked
Active
Viewed 298 times
0
-
so I think there are some application exist to doing so, and the question is, are you allowed to run the application at the server?! – Oct 23 '13 at 20:50
-
Lets assume I do have access... could you recommend anything in particular? – CMR Oct 24 '13 at 19:53
-
so this is going to easy, just follow the link you have provided, you just need to listen/monitor the file(s) you are trying to monitor, but if you ask me, and this is possible, I suggest you do not let user in the network access the file directly, in-other word give the file to users with a proxy module, like ftp or http protocol, and doing so for manipulating, this ensures you to manage and monitor the file(s) you want dynamically and with more flexible and reliable approach. – Oct 24 '13 at 21:50
1 Answers
0
I sincerely hope you have found a solution by now.
But assuming that the file in question is some kind of executable you could modify the example from this question: How to get a list of current open windows/process with Java?. And use JNA:
import com.sun.jna.Native;
import com.sun.jna.platform.win32.*;
import com.sun.jna.win32.W32APIOptions;
public class ProcessList {
public static void main(String[] args) {
WinNT winNT = (WinNT) Native.loadLibrary(WinNT.class, W32APIOptions.UNICODE_OPTIONS);
WinNT.HANDLE snapshot = winNT.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();
while (winNT.Process32Next(snapshot, processEntry)) {
//see if your process is open and update your counter
}
winNT.CloseHandle(snapshot);
}
}
and poll the system with a Timer
object.