Results 1 to 7 of 7

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

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

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

    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,

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

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

    System.IO.FileInfo class has a CreationTime property.

    Edit:
    Sorry, misinterpreted your post.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

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

    Does System.IO.FileInfo have a replacement in C# for CompareFileTime?

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

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

    The FileInfo.CreationTime property returns a full System.DateTime object so you can use the DateTime.Compare method.

    In your case,. FileInfo.LastWriteTime property which is the modified time, or FileInfo.LastWriteTimeUtc (universal coordinate time) may be more appropriate.

    http://msdn2.microsoft.com/en-us/library/5ata5aya.aspx

    Code:
    static void Main(string[] args)
    		{
    			DateTime t = DateTime.Now;
    			System.Threading.Thread.Sleep(10);
    
    			DateTime x = DateTime.Now;
    
    			if (t > x) Console.WriteLine("That's not right.");
    			else if (t < x)
    				Console.WriteLine("That's better.");
    			else 
    				Console.WriteLine("They're equal.");
    
    			Console.ReadLine();
    
    		}
    Last edited by nemaroller; Jul 19th, 2007 at 02:41 PM.

  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

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

    Of course, you can just compare the property values (note that UTC is good for comparing files on different machines or across networks, but may take a performance hit to convert the time to UTC - otherwise use LastWriteTime. You'll have to test that.):
    Code:
    static void Main(string[] args)
    		{
    
    
    			System.IO.FileInfo info1 = new System.IO.FileInfo(@"C:\testfile1.txt");
    			System.IO.FileInfo info2 = new System.IO.FileInfo(@"C:\testfile2.txt");
    
    			if (info1.LastWriteTimeUtc > info2.LastWriteTimeUtc)
    				Console.WriteLine("testfile1 is newer.");
    			else if (info1.LastWriteTimeUtc < info2.LastWriteTimeUtc )
    				Console.WriteLine("testfile2 is newer.");
    			else 
    				Console.WriteLine("They're equal.");
    
    			Console.ReadLine();
    
    		}
    Last edited by nemaroller; Jul 19th, 2007 at 02:53 PM.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

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

    Sadly I am not using FileInfo but FILETIME instead - I am not actually comparing FILES but creation time from a REGISTRY KEY using my GetFileTime(&ft); function - there is no FileInfo equivalent so I needed to create FILETIME variables and I need to compare those...

    So I really need to find a way to compare FILETIME's, if I can convert them into something else great but I can't change the fact that I am started with 2 FILETIME variables...

    Thanks,

  7. #7
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width