I found this signature on PInvoke
[DllImport("Kernel32.dll", SetLastError = true)] private static extern bool SetFileInformationByHandle( IntPtr hFile, int FileInformationClass, IntPtr lpFileInformation, Int32 dwBufferSize);
Somehow it didn't work. I had to change the parameter type of lpFileInformation to FILE_BASIC_INFO to make it work.
This is a complete C # example called from PowerShell:
$fu = @" using System; using System.IO; using System.Runtime.InteropServices; public class FileUtility { private struct FILE_BASIC_INFO { [MarshalAs(UnmanagedType.I8)] public Int64 CreationTime; [MarshalAs(UnmanagedType.I8)] public Int64 LastAccessTime; [MarshalAs(UnmanagedType.I8)] public Int64 LastWriteTime; [MarshalAs(UnmanagedType.I8)] public Int64 ChangeTime; [MarshalAs(UnmanagedType.U4)] public UInt32 FileAttributes; } [DllImport("Kernel32.dll", SetLastError = true)] private static extern bool SetFileInformationByHandle( IntPtr hFile, int FileInformationClass, FILE_BASIC_INFO lpFileInformation, Int32 dwBufferSize); public void SetFileChangeTime() { using (FileStream fs = new FileStream(@"c:\path\to\file", FileMode.Open)) { FILE_BASIC_INFO fileInfo = new FILE_BASIC_INFO(); fileInfo.ChangeTime = 943044610000000; SetFileInformationByHandle( fs.Handle, 0, // the same as FILE_INFO_BY_HANDLE_CLASS.FileBasicInfo fileInfo, Marshal.SizeOf(fileInfo)); } } } "@ Add-Type -TypeDefinition $fu -IgnoreWarnings $f = New-Object -TypeName FileUtility $f.SetFileChangeTime()
I ran an example with other date properties, as they are shown in Explorer, and it worked.
Edit
This code does not run in debug mode in VS. As mentioned above, this is an exception. Running EXE on the command line does not raise an exception. But the date of change is not updated. However, it only works in PowerShell. Itβs strange.
Yavuz source share