Directory.GetFiles() in Modified Date/Time Order Pt2

Following on from this.  I've noticed that it only gives you to the second accuracy.  If your software is capable of making more than one file per second, the sort order will be date/time to the second then alphanumerically on the filename.  Not ideal if your filenames are guids, the following class gives date/time sort accuracy of 100 nanosecond intervals then alphanumerically on filename :-

   1:  Imports System.IO
   2:  Imports System.Runtime.InteropServices
   3:  Imports System.Runtime.InteropServices.ComTypes
   4:  Imports Microsoft.Win32.SafeHandles
   5:  Imports Microsoft.Win32
   6:  Public Class clsCompareFileInfo
   7:      Implements IComparer
   8:   
   9:      Private Const GENERIC_READ = &H80000000
  10:      Private Const OPEN_EXISTING = 3
  11:      Private Const FILE_SHARE_READ = &H1
  12:      Private Const FILE_SHARE_WRITE = &H2
  13:      Private Const FILE_ATTRIBUTE_NORMAL = &H80
  14:      Private Const FILE_FLAG_NO_BUFFERING = &H20000000
  15:   
  16:      Public Structure FILETIME
  17:          Public dwLowDateTime As Long
  18:          Public dwHighDateTime As Long
  19:      End Structure
  20:   
  21:      Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Integer, ByRef lpCreationTime As ComTypes.FILETIME, ByRef lpLastAccessTime As ComTypes.FILETIME, ByRef lpLastWriteTime As ComTypes.FILETIME) As Integer
  22:   
  23:      Private Declare Auto Function CreateFile Lib "kernel32.dll" (ByVal lpFileName As String, _
  24:  ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, _
  25:  ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As Integer, _
  26:  ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As IntPtr) As IntPtr
  27:   
  28:      Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  29:      <DllImport( _
  30:                  "kernel32.dll", _
  31:                  CharSet:=CharSet.Auto, _
  32:                  SetLastError:=True)> _
  33:              Friend Shared Function CompareFileTime( _
  34:              ByRef lpFileTime1 As ComTypes.FILETIME, _
  35:              ByRef lpFileTime2 As ComTypes.FILETIME) _
  36:              As Integer
  37:      End Function
  38:   
  39:      Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
  40:          Dim File1 As FileInfo
  41:          Dim File2 As FileInfo
  42:   
  43:          File1 = DirectCast(x, FileInfo)
  44:          File2 = DirectCast(y, FileInfo)
  45:   
  46:          Dim x_Created As ComTypes.FILETIME
  47:          Dim x_Modified As ComTypes.FILETIME
  48:          Dim x_Accessed As ComTypes.FILETIME
  49:          Dim y_Created As ComTypes.FILETIME
  50:          Dim y_Modified As ComTypes.FILETIME
  51:          Dim y_Accessed As ComTypes.FILETIME
  52:   
  53:          Dim fhandle As IntPtr = CreateFile(File1.FullName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
  54:          GetFileTime(fhandle, x_Created, x_Accessed, x_Modified)
  55:   
  56:          Dim fhandle2 As IntPtr = CreateFile(File2.FullName, GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero)
  57:          GetFileTime(fhandle2, y_Created, y_Accessed, y_Modified)
  58:   
  59:          CloseHandle(fhandle)
  60:          CloseHandle(fhandle2)
  61:   
  62:          Compare = CompareFileTime(x_Modified, y_Modified)
  63:      End Function
  64:  End Class

Print | posted on Thursday, July 10, 2008 9:19 PM

Feedback

No comments posted yet.

Your comment:





 
 

Copyright © Neil Smith

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski