1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using Microsoft.Win32.SafeHandles;
6 using System;
7 using System.Runtime.InteropServices;
8 
9 internal partial class Interop
10 {
11     internal partial class Kernel32
12     {
13         // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365539.aspx
14         [DllImport(Libraries.Kernel32, SetLastError = true, ExactSpelling = true)]
SetFileInformationByHandle(SafeFileHandle hFile, FILE_INFO_BY_HANDLE_CLASS FileInformationClass, ref FILE_BASIC_INFO lpFileInformation, uint dwBufferSize)15         internal static extern bool SetFileInformationByHandle(SafeFileHandle hFile, FILE_INFO_BY_HANDLE_CLASS FileInformationClass, ref FILE_BASIC_INFO lpFileInformation, uint dwBufferSize);
16 
17         // Default values indicate "no change".  Use defaults so that we don't force callsites to be aware of the default values
SetFileTime( SafeFileHandle hFile, long creationTime = -1, long lastAccessTime = -1, long lastWriteTime = -1, long changeTime = -1, uint fileAttributes = 0)18         internal static unsafe bool SetFileTime(
19             SafeFileHandle hFile,
20             long creationTime = -1,
21             long lastAccessTime = -1,
22             long lastWriteTime = -1,
23             long changeTime = -1,
24             uint fileAttributes = 0)
25         {
26             FILE_BASIC_INFO basicInfo = new FILE_BASIC_INFO()
27             {
28                 CreationTime = creationTime,
29                 LastAccessTime = lastAccessTime,
30                 LastWriteTime = lastWriteTime,
31                 ChangeTime = changeTime,
32                 FileAttributes = fileAttributes
33             };
34 
35             return SetFileInformationByHandle(hFile, FILE_INFO_BY_HANDLE_CLASS.FileBasicInfo, ref basicInfo, (uint)sizeof(FILE_BASIC_INFO));
36         }
37 
38         internal struct FILE_BASIC_INFO
39         {
40             internal long CreationTime;
41             internal long LastAccessTime;
42             internal long LastWriteTime;
43             internal long ChangeTime;
44             internal uint FileAttributes;
45         }
46     }
47 }
48