Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Convert to DateTime object

Posted on 2004-08-19
5
Medium Priority
?
645 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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

971 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