Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How We can Retrieve File’s Time Information Using Win32 System Services :

Author: DevASP
Download Source Code : 616_FilesTimeInfo.zip

Windows API’s have a bundle of function that can be used to retrieve file’s information such as when did this file created or what attributes are set to this file (e.g. is this file is Hidden mode etc.

In this article I will discuss the Win32 API’s used to get the time information about files. Several functions required the Handle to the file, which we can Get through the API named CreateFile().

Getting the File time:

 

GetFileTime() is used to Get the Time information for an Opened file. This function Get three type of time information.

1-Creation time

2-Last Access time

3-Last Write time

 

The signature of this function looks like this.

 

 

BOOL GetFileTime(HANDLE file,LPFILETIME creationTime,

LPFILETIME lastAccessTime,LPFILETIME lastWriteTime); 

 

 

We can get the handle of a file through CreateFile() API. Then we can use this handle to get the time information of files.FILETIME is a structure that contains two 32-bit values. The 64 bits together represent the number of 100-nanosecond time increments that have passed since January 1, 1601.

First of all we will write a function which will display the time on a message box.

 

 

void DisplayTime(FILETIME fileTime, char* DescOfTime)

{

      FILETIME ft;

      SYSTEMTIME systemtime;

      FileTimeToLocalFileTime(&fileTime, &ft);

      FileTimeToSystemTime(&ft, &systemtime);

      char temp[150];

      sprintf(temp,"%d/%d/%d/%d/%d",systemtime.wMonth,systemtime.wDay,   systemtime.wYear,systemtime.wHour,systemtime.wMinute);

      MessageBox(temp , DescOfTime , MB_OK); 

 

} 

 

 

The FileTimeToLocalTime and FileTimeToSystemTime functions convert the 64-bit value to local time and then to a form suitable for output.I am going to write a function which you can call against any button handle to try it.This function will call the GetFileTime () to get the Creation time, Last Access time and Last write time. I have a file named “TestFile.txt” in my project folder (Current Directory).

 

This function will be look like this.

 

 

void GetTime(void)

{

      HANDLE fileHandle;

      char filename[] = "TestFile.txt";

      FILETIME createt, lastWriteTime, lastAccessTime;

      BOOL succeeded;

      FILETIME filetime;

      SYSTEMTIME systemtime;

 

      fileHandle = CreateFile(filename, GENERIC_READ,

            FILE_SHARE_WRITE,

            0, OPEN_EXISTING, 0, 0);

      if (fileHandle == INVALID_HANDLE_VALUE)

      {

            MessageBox("Cant open file");

            return;

      }

      else

      {

            // get the file times

            succeeded = GetFileTime( fileHandle, & createt, & lastAccessTime, & lastWriteTime);

 

            DisplayTime(lastWriteTime,"Last Write Time");

            // the other two won't work in FAT systems

            DisplayTime(createt,"File Creation Time");

            DisplayTime(lastAccessTime, "File Last Access time");

 

      }

      CloseHandle(fileHandle);

 

} 

 

 

This function will display the Last write time to file in first message box, File create time in second message box and File Last access time in third message box.The FAT file system supports only the last write time, but NTFS and HPFS support all three.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Writing an application on Basic File Operations Using Win32 System Services

Disclaimer - Privacy
© 2002-2017 DevASP.net