Solved

Convert to DateTime object

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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