using System;
using System.IO;
using System.Runtime.InteropServices;
public struct FileTime
{
public uint dwLowDateTime;
public uint dwHighDateTime;
public static implicit operator long(FileTime fileTime)
{
long returnedLong;
byte[] highBytes = BitConverter.GetBytes(fileTime.dwHighDateTime);
Array.Resize(ref highBytes, 8);
returnedLong = BitConverter.ToInt64(highBytes, 0);
returnedLong = returnedLong << 32;
returnedLong = returnedLong | fileTime.dwLowDateTime;
return returnedLong;
}
}
public class FileTimes
{
private const int OPEN_EXISTING = 3;
private const int INVALID_HANDLE_VALUE = -1;
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
private static extern int CreateFile(string lpFileName,
int dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile);
[DllImport("Kernel32.dll")]
private static extern bool GetFileTime(int hFile,
out FileTime lpCreationTime,
out FileTime lpLastAccessTime,
out FileTime lpLastWriteTime);
[DllImport("Kernel32.dll")]
private static extern bool CloseHandle(int hFile);
public static void Main()
{
string winDir = Environment.SystemDirectory;
if (! (winDir.EndsWith(Path.DirectorySeparatorChar.ToString())))
winDir += Path.DirectorySeparatorChar;
winDir += "write.exe";
int hFile = CreateFile(winDir, 0, 0, 0, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE)
{
Console.WriteLine("Unable to access {0}.", winDir);
}
else
{
FileTime creationTime, accessTime, writeTime;
if (GetFileTime(hFile, out creationTime, out accessTime, out writeTime))
{
CloseHandle(hFile);
long fileCreationTime = (long) creationTime;
long fileAccessTime = accessTime;
long fileWriteTime = (long) writeTime;
Console.WriteLine(DateTimeOffset.FromFileTime(fileCreationTime).ToString());
Console.WriteLine(DateTimeOffset.FromFileTime(fileAccessTime).ToString());
Console.WriteLine(DateTimeOffset.FromFileTime(fileWriteTime).ToString());
}
}
FileInfo fileInfo = new FileInfo(winDir);
DateTimeOffset infoCreationTime, infoAccessTime, infoWriteTime;
long ftCreationTime, ftAccessTime, ftWriteTime;
infoCreationTime = fileInfo.CreationTime;
infoAccessTime = fileInfo.LastAccessTime;
infoWriteTime = fileInfo.LastWriteTime;
ftCreationTime = infoCreationTime.ToFileTime();
ftAccessTime = infoAccessTime.ToFileTime();
ftWriteTime = infoWriteTime.ToFileTime();
Console.WriteLine(DateTimeOffset.FromFileTime(ftCreationTime).ToString());
Console.WriteLine(DateTimeOffset.FromFileTime(ftAccessTime).ToString());
Console.WriteLine(DateTimeOffset.FromFileTime(ftWriteTime).ToString());
}
}