c# - FileSystemWatcher used to watch for folder/file open -



c# - FileSystemWatcher used to watch for folder/file open -

i have browsed around cannot find info on seeking, if there post goes on apologize.

i seeking help code monitor specific folder when folder opened person or when file under said folder opened. @ point can see when user opens , modifies files if open file view it, not throw event when add together lastaccessed. info or help appreciated.

folder name c:\junk

code in c# 4.0:

[permissionset(securityaction.demand, name = "fulltrust")] public static void run() { filesystemwatcher watcher = new filesystemwatcher(); watcher.path = @"c:\"; watcher.notifyfilter = notifyfilters.lastaccess | notifyfilters.lastwrite | notifyfilters.filename | notifyfilters.directoryname; watcher.filter = "junk"; // add together event handlers. watcher.changed += new filesystemeventhandler(onchanged); watcher.created += new filesystemeventhandler(onchanged); watcher.deleted += new filesystemeventhandler(onchanged); watcher.renamed += new renamedeventhandler(onrenamed); watcher.includesubdirectories = true; watcher.enableraisingevents = true; // wait user quit program. console.writeline("press \'q\' quit sample."); while (console.read() != 'q') ; } // define event handlers. private static void onchanged(object source, filesystemeventargs e) { // specify done when file changed, created, or deleted. console.writeline("file: " + e.fullpath + " " + e.changetype); } private static void onrenamed(object source, renamedeventargs e) { // specify done when file renamed. console.writeline("file: {0} renamed {1}", e.oldfullpath, e.fullpath); }

it not throw event when add together lastaccessed.

because notifyfilters.lastaccessed specifies wish retreive property, not event subscribe to. available events changed, created, or deleted, none of want.

you should take @ readdirectorychangesw win32 function, documented here. can passed file_notify_change_last_access flag, seems deliver want:

any alter lastly access time of files in watched directory or subtree causes alter notification wait operation return.

edit: disregard this, filesystemwatcher internally pass notifyfilters.lastwrite int 32, same file_notify_change_last_access, readdirectorychangesw. function still not notify on file access, i've tried.

perhaps caused this:

last access time has loose granularity guarantees time accurate within 1 hour. in windows vista, we've disabled updates lastly access time improve ntfs performance. if using application relies on value, can enable using next command:

fsutil behavior set disablelastaccess 0

you must restart computer alter take effect.

if execute on command prompt, perhaps lastaccess written , event fire. i'm not going seek in on ssd , don't have vm ready, on windows 7 disablelastaccess seems enabled out-of-the-box.

if still doesn't work when disable behavior, wait raymond chen's suggestion box (or himself) come by, there's quite logical explanation why documentation not seem correctly describe behaviour encounter. ;-)

you may scan directory in loop , @ lastaccessed property of files. trying when user opens file?

c# .net wpf filesystemwatcher

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -