diff --git a/src/libraries/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs b/src/libraries/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs index 4b5867e540899f..69792e7eac6510 100644 --- a/src/libraries/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs +++ b/src/libraries/System.IO.FileSystem/src/System/IO/DirectoryInfo.cs @@ -235,8 +235,12 @@ public void MoveTo(string destDirName) Invalidate(); } - public override void Delete() => FileSystem.RemoveDirectory(FullPath, recursive: false); + public override void Delete() => Delete(recursive: false); - public void Delete(bool recursive) => FileSystem.RemoveDirectory(FullPath, recursive); + public void Delete(bool recursive) + { + FileSystem.RemoveDirectory(FullPath, recursive); + Invalidate(); + } } } diff --git a/src/libraries/System.IO.FileSystem/src/System/IO/FileInfo.cs b/src/libraries/System.IO.FileSystem/src/System/IO/FileInfo.cs index 6a69fcbcc7ec14..4857812d2b3402 100644 --- a/src/libraries/System.IO.FileSystem/src/System/IO/FileInfo.cs +++ b/src/libraries/System.IO.FileSystem/src/System/IO/FileInfo.cs @@ -99,9 +99,18 @@ public FileInfo CopyTo(string destFileName, bool overwrite) return new FileInfo(destinationPath, isNormalized: true); } - public FileStream Create() => File.Create(NormalizedPath); + public FileStream Create() + { + FileStream fileStream = File.Create(NormalizedPath); + Invalidate(); + return fileStream; + } - public override void Delete() => FileSystem.DeleteFile(FullPath); + public override void Delete() + { + FileSystem.DeleteFile(FullPath); + Invalidate(); + } public FileStream Open(FileMode mode) => Open(mode, (mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite), FileShare.None); diff --git a/src/libraries/System.IO.FileSystem/tests/Base/InfoGetSetTimes.cs b/src/libraries/System.IO.FileSystem/tests/Base/InfoGetSetTimes.cs index 0e61c82cd5187d..9973fe9cdba9f1 100644 --- a/src/libraries/System.IO.FileSystem/tests/Base/InfoGetSetTimes.cs +++ b/src/libraries/System.IO.FileSystem/tests/Base/InfoGetSetTimes.cs @@ -49,7 +49,7 @@ public void ExistsThenDelete_ReturnsDefaultValues() } [Fact] - public void TimesStillSetAfterDelete() + public void TimesResetAfterDelete() { T item = GetExistingItem(); @@ -63,11 +63,13 @@ public void TimesStillSetAfterDelete() times.Add(timeFunction, timeFunction.Getter(item)); } - // Deleting shouldn't change any info state + // Deleting should refresh state item.Delete(); + Assert.All(times, time => { - Assert.Equal(time.Value, time.Key.Getter(item)); + // We check that all the file times have been refreshed + Assert.NotEqual(time.Key.Getter(item), time.Value); }); } } diff --git a/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/Create.cs b/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/Create.cs index 3eef2db07abe17..5ca55487405218 100644 --- a/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/Create.cs +++ b/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/Create.cs @@ -83,5 +83,14 @@ public void CreateDirectoryWithAttributes() testInfo.Create(); testInfo.Attributes = FileAttributes.Directory | FileAttributes.Normal; } + + [Fact] + public void CreateInvalidatesDirectoryInfo() + { + string testDir = Path.Combine(GetTestFilePath(), "DirectoryCreate"); + DirectoryInfo testDirectoryInfo = new DirectoryInfo(testDir); + testDirectoryInfo.Create(); + Assert.True(testDirectoryInfo.Exists); + } } } diff --git a/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/Delete.cs b/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/Delete.cs index 09082d1553f320..1790bd9b2e8f38 100644 --- a/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/Delete.cs +++ b/src/libraries/System.IO.FileSystem/tests/DirectoryInfo/Delete.cs @@ -14,17 +14,14 @@ protected override void Delete(string path) } [Fact] - public void ExistsDoesntRefreshOnDelete() + public void DeleteInvalidatesDirectoryInfo() { - DirectoryInfo dir = Directory.CreateDirectory(Path.Combine(TestDirectory, Path.GetRandomFileName())); - - Assert.True(dir.Exists); - - dir.Delete(); - - Assert.True(dir.Exists); - dir.Refresh(); - Assert.False(dir.Exists); + string testDir = Path.Combine(GetTestFilePath(), "DirectoryCreate"); + DirectoryInfo testDirectoryInfo = new DirectoryInfo(testDir); + testDirectoryInfo.Create(); + Assert.True(testDirectoryInfo.Exists); + testDirectoryInfo.Delete(); + Assert.False(testDirectoryInfo.Exists); } } diff --git a/src/libraries/System.IO.FileSystem/tests/FileInfo/Create.cs b/src/libraries/System.IO.FileSystem/tests/FileInfo/Create.cs index c526e3f770f3d9..f2c5c2b8643b65 100644 --- a/src/libraries/System.IO.FileSystem/tests/FileInfo/Create.cs +++ b/src/libraries/System.IO.FileSystem/tests/FileInfo/Create.cs @@ -32,6 +32,18 @@ public void FullNameUpdatesOnCreate() } } + [Fact] + public void FileInfoInvalidAfterCreate() + { + DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath()); + string testFilePath = Path.Combine(testDir.FullName, GetTestFileName()); + FileInfo info = new FileInfo(testFilePath); + using (FileStream fileStream = info.Create()) + { + Assert.True(info.Exists); + } + } + #endregion } } diff --git a/src/libraries/System.IO.FileSystem/tests/FileInfo/Delete.cs b/src/libraries/System.IO.FileSystem/tests/FileInfo/Delete.cs index 9a76d69e3b0cb5..0e14a283653eb5 100644 --- a/src/libraries/System.IO.FileSystem/tests/FileInfo/Delete.cs +++ b/src/libraries/System.IO.FileSystem/tests/FileInfo/Delete.cs @@ -12,5 +12,20 @@ protected override void Delete(string path) { new FileInfo(path).Delete(); } + + [Fact] + public void FileInfoInvalidatedOnDelete() + { + DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath()); + string testFilePath = Path.Combine(testDir.FullName, GetTestFileName()); + FileInfo info = new FileInfo(testFilePath); + using (FileStream fileStream = info.Create()) + { + Assert.True(info.Exists); + } + + info.Delete(); + Assert.False(info.Exists); + } } } diff --git a/src/libraries/System.IO.FileSystem/tests/FileInfo/GetSetTimes.cs b/src/libraries/System.IO.FileSystem/tests/FileInfo/GetSetTimes.cs index 9c6109107ca8a4..6fe85747d2de2d 100644 --- a/src/libraries/System.IO.FileSystem/tests/FileInfo/GetSetTimes.cs +++ b/src/libraries/System.IO.FileSystem/tests/FileInfo/GetSetTimes.cs @@ -194,9 +194,8 @@ public void DeleteAfterEnumerate_TimesStillSet() DateTime afterTime = DateTime.UtcNow.AddSeconds(1); - // Deleting doesn't change any info state info.Delete(); - ValidateSetTimes(info, beforeTime, afterTime); + Assert.Equal(DateTime.FromFileTimeUtc(0), info.LastAccessTimeUtc); }