Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GetFileTime (kernel32)
 
.
Summary

C# Signature:

[DllImport("kernel32.dll")]
static extern bool GetFileTime(IntPtr hFile, IntPtr lpCreationTime,
   IntPtr lpLastAccessTime, IntPtr lpLastWriteTime);

VB.Net Signature:

<DllImport( _
            "kernel32.dll", _
            CharSet:=CharSet.Auto, _
            SetLastError:=True)> _
         Friend Shared Function GetFileTime( _
                        ByVal hFile As IntPtr, _
                        <Out()> ByRef lpCreationTime As FILETIME, _
                        <Out()> ByRef lpLastAccessTime As FILETIME, _
                        <Out()> ByRef lpLastWriteTime As FILETIME) _
                    As Boolean
        End Function

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

const uint GENERIC_READ = 0x80000000;

const uint FILE_SHARE_READ = 0x1;

const uint FILE_ATTRIBUTE_NORMAL = 0x80;

const int INVALID_HANDLE_VALUE = -1;

const uint OPEN_EXISTING = 3;

[StructLayout(LayoutKind.Sequential)]

struct FILETIME

{

    public uint dwLowDateTime;
    public uint dwHighDateTime;
    public DateTime ToDateTime()
    {
    return DateTime.FromFileTimeUtc((((long)dwHighDateTime) << 32) | ((uint)dwLowDateTime));
    }

}

[DllImport("kernel32.dll", SetLastError = true)]

[return: MarshalAs(UnmanagedType.Bool)]

extern bool CloseHandle(

    IntPtr hObject

);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]

extern IntPtr CreateFile(

    string lpFileName,
    uint dwDesiredAccess,
    uint dwShareMode,
    IntPtr SecurityAttributes,
    uint dwCreationDisposition,
    uint dwFlagsAndAttributes,
    IntPtr hTemplateFile

);

[DllImport("kernel32.dll", SetLastError = true)]

extern bool GetFileTime(

    IntPtr hFile,
    ref FILETIME lpCreationTime,
    ref FILETIME lpLastAccessTime,
    ref FILETIME lpLastWriteTime

);

void GetFileTimes(string FileName, out DateTime CreationTime, out DateTime LastAccessTime, out DateTime LastWriteTime)

{

    CreationTime = DateTime.MinValue;
    LastAccessTime = DateTime.MinValue;
    LastWriteTime = DateTime.MinValue;
    IntPtr ptr = IntPtr.Zero;
    FILETIME ftCreationTime = new FILETIME();
    FILETIME ftLastAccessTime = new FILETIME();
    FILETIME ftLastWriteTime = new FILETIME();
    try
    {
    ptr = CreateFile(@"c:\1.txt", GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
    if (ptr.ToInt32() == INVALID_HANDLE_VALUE)
        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
    if (GetFileTime(ptr, ref ftCreationTime, ref ftLastAccessTime, ref ftLastWriteTime) != true)
        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
    CreationTime = DateTime.FromFileTimeUtc((((long)ftCreationTime.dwHighDateTime) << 32) | ((uint)ftCreationTime.dwLowDateTime));
    LastAccessTime = DateTime.FromFileTimeUtc((((long)ftLastAccessTime.dwHighDateTime) << 32) | ((uint)ftLastAccessTime.dwLowDateTime));
    LastWriteTime = DateTime.FromFileTimeUtc((((long)ftLastWriteTime.dwHighDateTime) << 32) | ((uint)ftLastWriteTime.dwLowDateTime));
    }
    catch (Exception e)
    {
    throw (e);
    }
    finally
    {
    if (ptr !=IntPtr.Zero && ptr.ToInt32() != INVALID_HANDLE_VALUE) CloseHandle(ptr);
    }

}

Alternative Managed API:

class System.IO.FileInfo,

members CreationTime and others.

Documentation
GetFileTime on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Find References
Show Printable Version
Revisions