tags:

views:

199

answers:

3

Does anybody know why FileAge is not working with "c:\pagefile.sys"? It returns -1.

Update:
Found it: It is a Delphi bug fixed in Delphi 2010 (QC Entry 73539), but the PDF I have found does not explain how they fix it.

Does anyone know how they fix it so I can fix my Delphi 7?

UPDATE: Elegant fix provided by Radu Barbu!


Delphi 7, Win 7 (32 bits)

A: 

Call GetLastError() to get the error code returned by FindFirstFile() API function (called by FileAge).

Update: Delphi 2010 fix falls back to FindFirstFile so most likely it won't help you. They call GetFileAttributesEx and if this fails, they call FindFirstFile. And GetFileAttributesEx is supposed to fail for pagefile.sys. So you do need to check the error code.

Eugene Mayevski 'EldoS Corp
Why is GetFileAttributesEx *supposed* to fail for pagefile.sys? Is that documented somewhere?
Rob Kennedy
@Rob Kennedy it's not supposed, it just fails. See reports on this in Google.
Eugene Mayevski 'EldoS Corp
+2  A: 

Note that FileAge is deprecated.

  TFile.GetLastAccessTime( FileName)  

might be a replacement...

Tom1952
The version of FileAge that takes 2 parameters is NOT deprecated. I have found the info on Internet.
Altar
+1  A: 

try this:

with a variable of type TSearchRec (wSr bellow) load pagefile.sys then

wSR.FindData.ftLastWriteTime - should return when the file was accessed

and with the function bellow you should get the time

function FileTime2DateTime(FileTime: TFileTime): TDateTime;
var
  LocalFileTime     : TFileTime;
  SystemTime        : TSystemTime;
begin
  Result := 0;
  try
    FileTimeToLocalFileTime(FileTime, LocalFileTime);
    FileTimeToSystemTime(LocalFileTime, SystemTime);
    Result := SystemTimeToDateTime(SystemTime);
  except on e: Exception do
//some message if you want
  end;
end;

best regards,

Radu Barbu
Wow. It worked. You should contact Embarcadero to let them know there is a lighter solution than the one used by them.
Altar
glad that i could help.
Radu Barbu