Filename of LastWriteTime
-
Hi - I am trying to get the filename of the last written file in a directory. Any ideas why this code doesn''t work?
Dim _direct As DirectoryInfo Dim _strLastfileName, _strDirectpath As String _strDirectpath = txtExportPath.Text ' path to directory _direct = New DirectoryInfo(_strDirectpath) _strLastfileName = New FileInfo(_direct.LastWriteTime).Name.ToString
-
Hi - I am trying to get the filename of the last written file in a directory. Any ideas why this code doesn''t work?
Dim _direct As DirectoryInfo Dim _strLastfileName, _strDirectpath As String _strDirectpath = txtExportPath.Text ' path to directory _direct = New DirectoryInfo(_strDirectpath) _strLastfileName = New FileInfo(_direct.LastWriteTime).Name.ToString
The only way to know which file was written to last is to get the LastAccess time for every file in the folder, sort the list and get the filename off the top of the sorted list. The LastWriteTime of the Directory is a change in the directory entries for that folder, not the last write to a file in that directory. Also, The FileInfo class constructor takes a filename as a string as an argument. You're supplying a DateTime object (_direct.LastWriteTime) as the argument, which won't work.
Dave Kreskowiak Microsoft MVP - Visual Basic