Solved

Convert to DateTime object

Posted on 2004-08-19
5
593 Views
Last Modified: 2012-06-21
I have a FileData object called myfile.

How do I convert the lastWriteTime_highDateTime and lastWriteTime_lowDateTime properties to a normal DateTime object?

Please ask if need more info.

Thanks
0
Comment
Question by:imt21
  • 3
5 Comments
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 11842094
Why not use FileInfo? i.e.

FileInfo fi = new FileInfo( "fullFilePath" );
DateTime dt = fi.LastWriteTime;

Other than that what type of object is lastWriteTime_*?
0
 
LVL 1

Expert Comment

by:chmohan
ID: 11842172
System.DateTime dtTime = new DateTime();
dtTime.Hour,dtTime.Minutes.dtTime.Year ,assign these values from your property values
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 250 total points
ID: 11842238
This should do the trick:

  [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError=true)] static extern int FileTimeToSystemTime(ref FILETIME lpFileTime, ref SystemTime lpSystemTime);

    struct FILETIME
    {
      public int dwLowDateTime;
      public int dwHighDateTime;
    }

    struct SystemTime
    {
      public short wYear;
      public short wMonth;
      public short wDayOfWeek;
      public short wDay;
      public short wHour;
      public short wMinute;
      public short wSecond;
      public short wMilliseconds;
    }

    static DateTime GetDateTime(int dwLowDateTime, int dwHighDateTime)
    {
      FILETIME fileTime = new FILETIME();
      fileTime.dwLowDateTime = dwLowDateTime;
      fileTime.dwHighDateTime = dwHighDateTime;

      SystemTime systemTime = new SystemTime();
      FileTimeToSystemTime(ref fileTime, ref systemTime);

      return new DateTime(systemTime.wYear, systemTime.wMonth, systemTime.wDay, systemTime.wHour, systemTime.wMinute, systemTime.wSecond, systemTime.wMilliseconds);
    }

Just call GetDateTime with your low and high time properties from your FileData object.
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11842591
imt21, could you provide a sample of the values in lastWriteTime_highDateTime, and lastWriteTime_lowDateTime along with the actual Date/Time reported by Windows?
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11842905
Actually, nevermind my last request - I found the values I needed to test.  The .NET Framework offers a method that will allow you to avoid another API call:

  DateTime.FromFileTimeUtc(dwLowDateTime + ((long)dwHighDateTime << 32))

Will return a DateTime object based on the low and high time properties from your FileData object (Which will match the FileTimeToSystemTime call).

Hope this helps!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now