Solved

Convert to DateTime object

Posted on 2004-08-19
5
602 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

23 Experts available now in Live!

Get 1:1 Help Now