Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to find the lastly open file name through a directory by windows application in C#. Its very urgent for me..
Posted
Updated 14-Sep-12 1:07am
v2
Comments
Malli_S 14-Sep-12 7:09am    
What kind of 'Lastly opened file name' ? Because usually, applications maintain their MRUs. And Windows maintains application specific MRUs in registry.

What about this code:
C#
var directory = new DirectoryInfo("C:\\Temp");
var myFile = (from f in directory.GetFiles()
              orderby f.LastWriteTime descending
              select f).First();

MessageBox.Show(myFile.Name.ToString());
 
Share this answer
 
Comments
Dharmendra-18 14-Sep-12 7:12am    
but this code is not working i have already use fileinfo , fliesysteminfo, fliesystemwatcher class
JF2015 14-Sep-12 7:14am    
So, what do you mean with "not working"? It is working on my PC...
Dharmendra-18 14-Sep-12 7:31am    
I mean i want to get file name which is recently open in a directory
JF2015 14-Sep-12 7:37am    
The above code gives you the file that was last written to. You can also read which file was last accessed by using f.LastAccessTime or created by using f.CreationTime. In the file properties there is no "last opened".
As "opening" a file might occur for various reasons, you have several means depending on the operation: let's say read or write. And let's suppose you want to watch an NTFS file system.

You can take a look at the following:
- USN (write): Eyes on NTFS[^], http://www.donationcoder.com/forum/index.php?topic=22695[^]
- Audit trail (read/write) (http://support.microsoft.com/kb/310399[^]), and reading the logs
- FileSystemWatcher[^] (write)
- EyeHook[^] (read/write)
- and of course, you can use FileInfo.LastAccessTime[^] (or LastWriteTime[^]). LastAccessTime will only be relevant if it was it's tracking was not deactivated!
 
Share this answer
 
v3
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900