Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a File Based on Last Access Time

Status
Not open for further replies.

Yoxy99

MIS
Jul 28, 2007
23
US
Hello, I have a program that reads a text file with streamreader, does anyone know of a way to read a file based on when it was last accessed?
 

IO.File.GetLastAccessTime(Filepath) should give you the info you need.

You can also use a filesystem watcher if you want to automatically read a file whenever it's changed.

 
I guess my next question would be how would I compare the FileAccess times and find the File that was accessed most recently?
 


An old school bubble sort will work.
Just create a variable to hold the latest file path. Then loop through files in your directory.
Something like this:

Code:
	 Public Function LastAccessedFile(ByVal strDir As String) As String
		  Dim strRes As String		 [green]' path to last file accessed[/green]
		  Dim tLast As DateTime		 [green]' time of last access[/green]
		  Dim tCur As DateTime		 [green]' last access time of current file in the loop[/green]

		  For Each strFile As String In Directory.GetFiles(strDir)
				[green]' get the curent file's last access time[/green]
				tCur = File.GetLastAccessTime(strFile)

				[green]' grab the current file if this is the first iteration
				' or if it has the most recent access date so far[/green]
				If (tCur > tLast OrElse strRes Is Nothing) Then
					 strRes = strFile
					 tLast = tCur
				End If
		  Next

		  Return strRes

	 End Function

You'll have to add error handling and code to deal with empty folders, etc... but it should give you a place to start.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top