Comparing two FILETIME to see which is newer (CompareFileTime) [C# 2002]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
I have two FILETIME (using System.Runtime.InteropServices;) variables that contain File Times and I need to compare the two to find out who is never. In the

past (VC++6) I would simply use [-1 == CompareFileTime (&mrft, &ft)] but this doesn't seem to exist in C# [2002] nor am I able to find its replacement. So

how does one go about performing a CompareFileTime of two FileTime variables in C#?

Code:
FILETIME ft, ftMostRecent;
ftMostRecent.dwLowDateTime = 0;
ftMostRecent.dwHighDateTime = 0;

GetFileTime(&ft);
if (-1 == CompareFileTime (&ftMostRecent, &ft))
{
	// ... do something ... //
}

Specifically I need to know if "ft" is more recent then "ftMostRecent"...
Do I (and even can I) DLLImport the function itself (CompareFileTime)? If so how would the DllImport declaration go? And if not what else I can use or what other method can I utilize to get the same result?

Any help would be greatly appreciated.
Thanks,
 
You can import the CompareFileTime function from the Windows API to use it in C#.


C# Code[HorizontalRule]magic hidden text[/HorizontalRule][DllImport("kernel32.dll")]
static extern int CompareFileTime (
____ ref FILETIME lpFileTime1,
____ ref FILETIME lpFileTime2);
[HorizontalRule]Why are you quoting me?[/HorizontalRule]

Note that the reference operator in C++ becomes the ref keyword in C#...


C# Code[HorizontalRule]magic hidden text[/HorizontalRule]if (-1 == CompareFileTime (ref ftMostRecent, ref ft))
{
____ // ... do something ... //
}
[HorizontalRule]Why are you quoting me?[/HorizontalRule]
 
well you can use a couple of easy ways
way 1:
Code:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][SIZE=2].WriteLine([/SIZE][SIZE=2][COLOR=#2b91af]DateTime[/COLOR][/SIZE][SIZE=2].Compare(System.IO.[/SIZE][SIZE=2][COLOR=#2b91af]File[/COLOR][/SIZE][SIZE=2].GetCreationTime([/SIZE][SIZE=2][COLOR=#a31515]"C:\\B156_FSM.pdf"[/COLOR][/SIZE][SIZE=2]), System.IO.[/SIZE][SIZE=2][COLOR=#2b91af]File[/COLOR][/SIZE][SIZE=2].GetCreationTime([/SIZE][SIZE=2][COLOR=#a31515]"C:\\YServer.txt"[/COLOR][/SIZE][SIZE=2])));
[/SIZE]
basically you will get a return of 1 or -1 ( maybe 0 if both had identical times, but don't quote me on that )
way 2:
Code:
[SIZE=2][/SIZE][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][SIZE=2].WriteLine( [/SIZE][SIZE=2][COLOR=#2b91af]DateTime[/COLOR][/SIZE][SIZE=2].Compare([/SIZE][SIZE=2][COLOR=#2b91af]DateTime[/COLOR][/SIZE][SIZE=2].FromFileTime(' the long filetime value here !!! '), [/SIZE][SIZE=2][COLOR=#2b91af]DateTime[/COLOR][/SIZE][SIZE=2].FromFileTime(' the long filetime value here !!! ')) );
[/SIZE][SIZE=2][COLOR=#008000][/COLOR][/SIZE]
hope it helps.
 
Back
Top