diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.Windows.cs index c49f98c6aab03..56c4edcff5eac 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.Windows.cs @@ -464,10 +464,10 @@ public static unsafe void SetAttributes(SafeFileHandle fileHandle, FileAttribute private static void SetFileTime( string fullPath, bool asDirectory, - long creationTime = -1, - long lastAccessTime = -1, - long lastWriteTime = -1, - long changeTime = -1, + long creationTime = 0, + long lastAccessTime = 0, + long lastWriteTime = 0, + long changeTime = 0, uint fileAttributes = 0) { using SafeFileHandle handle = OpenHandleToWriteAttributes(fullPath, asDirectory); @@ -477,10 +477,10 @@ private static void SetFileTime( private static unsafe void SetFileTime( SafeFileHandle fileHandle, string? fullPath = null, - long creationTime = -1, - long lastAccessTime = -1, - long lastWriteTime = -1, - long changeTime = -1, + long creationTime = 0, + long lastAccessTime = 0, + long lastWriteTime = 0, + long changeTime = 0, uint fileAttributes = 0) { var basicInfo = new Interop.Kernel32.FILE_BASIC_INFO diff --git a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/GetSetTimes_SafeFileHandle.cs b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/GetSetTimes_SafeFileHandle.cs index 47147428379d5..9d6dc5ef6225c 100644 --- a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/GetSetTimes_SafeFileHandle.cs +++ b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/GetSetTimes_SafeFileHandle.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Threading.Tasks; using Microsoft.Win32.SafeHandles; using Xunit; @@ -91,15 +92,34 @@ protected override DateTime GetLastWriteTimeUtc(string path) return File.GetLastWriteTimeUtc(fileHandle); } + [Fact] + public async Task WritingShouldUpdateWriteTime_After_SetLastAccessTime() + { + string filePath = GetTestFilePath(); + using var handle = OpenFileHandle(filePath, FileAccess.ReadWrite); + + File.SetLastAccessTime(handle, DateTime.Now.Subtract(TimeSpan.FromDays(1))); + var timeBeforeWrite = File.GetLastWriteTime(handle); + + using var writer = new StreamWriter(new FileStream(handle, FileAccess.ReadWrite)); + writer.AutoFlush = true; + writer.WriteLine("now: " + DateTime.Now); + await Task.Delay(2000); + writer.WriteLine("now: " + DateTime.Now); + + var timeAfterWrite = File.GetLastWriteTime(handle); + Assert.True(timeAfterWrite > timeBeforeWrite); + } + [Fact] public void NullArgumentValidation() { Assert.Throws("fileHandle", static () => File.GetCreationTime(default(SafeFileHandle)!)); Assert.Throws("fileHandle", static () => File.SetCreationTime(default(SafeFileHandle)!, DateTime.Now)); - + Assert.Throws("fileHandle", static () => File.GetCreationTimeUtc(default(SafeFileHandle)!)); Assert.Throws("fileHandle", static () => File.SetCreationTimeUtc(default(SafeFileHandle)!, DateTime.Now)); - + Assert.Throws("fileHandle", static () => File.GetLastAccessTime(default(SafeFileHandle)!)); Assert.Throws("fileHandle", static () => File.SetLastAccessTime(default(SafeFileHandle)!, DateTime.Now));