Click here to Skip to main content
12,896,953 members (54,036 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#

I am trying to retrieve the LastWrite time from the Registry key that stores USB mounted device information and I'm having problems with the conversion to DateTime.

I am using the RegQueryInfoKey[^] API and my code to retireve a DateTime object is:

 // override RegQueryInfoKey. lpftLastWriteTime represented by int array
queryInfoKey = RegQueryInfoKey(hSubKey, null, null, IntPtr.Zero,
                ref lpcSubKeys, null, null, ref lpcValues, null, null, null, lpftLastWriteTime);
// subtract the high datetime form the low datetime. low datetime is negative
long lastWriteTime = (((long)lpftLastWriteTime[1]) << 32) + (long)lpftLastWriteTime[0];
// convert long to DateTime object
DateTime lastWrite = DateTime.FromFileTime(lastWriteTime);

However, this code does not return the correct LastWrite time in every case.

Can someone shed a bit of light on this?


Posted 10-Aug-10 23:37pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Just to answer my own question :-D , the problem lies with the definition of the FILETIME structure. The Win32 structure uses DWORD members, which are UNSIGNED, whereas the .NET Framework type uses SIGNED ints.

See this article for more information :thumbsup:!1A9E939F7373F3B7!146.entry?sa=683367813[^]

I'm suprised no one has come across this problem before!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 26 Aug 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100