From d41cf032ee66098576da82891cbe2b7b076aff8b Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Wed, 23 Sep 2026 14:08:24 +0200 Subject: [PATCH] Check if a path is a file or a directory: retarget net10.0, add Path.Exists, guard File.GetAttributes - Both projects net7.0 to net10.0. - Tests: Microsoft.NET.Test.Sdk 17.7.1 to 18.10.1, MSTest.TestAdapter and MSTest.TestFramework 2.2.10 replaced by the MSTest 4.4.1 meta-package, coverlet.collector 3.2.0 to 10.0.1. Assert.ThrowsException renamed to Assert.ThrowsExactly at the one call site MSTest 4 breaks. - FileAttributesUsage: the try/catch that caught only FileNotFoundException becomes a Path.Exists() guard. File.GetAttributes() throws DirectoryNotFoundException when a parent directory is missing, which the old catch let through. - File.CreateText() replaced by File.WriteAllText(path, string.Empty) in seven files, so the StreamWriter is no longer left open. - FileInfoAndDirectoryInfoDrawbacks now calls Refresh() and prints the fresh answer after the stale one. - New PathExistsUsage demonstration and PathTypeWithPathExistsIntegrationTest. - Unused usings removed; three Run()-only classes made static. --- .../CheckPathFileOrDirectoryCSharp.csproj | 2 +- .../FileAndDirectoryUsage.cs | 2 +- .../FileAttributesUsage.cs | 17 ++++---- .../FileInfoAndDirectoryInfoDrawbacks.cs | 19 ++++---- .../FileInfoAndDirectoryInfoUsage.cs | 4 +- .../PathExistsUsage.cs | 43 +++++++++++++++++++ .../CheckPathFileOrDirectoryCSharp/Program.cs | 1 + .../PathTypeWithAttributesIntegrationTest.cs | 4 +- ...TypeWithFileAndDirectoryIntegrationTest.cs | 2 +- ...FileInfoAndDirectoryInfoIntegrationTest.cs | 2 +- .../PathTypeWithPathExistsIntegrationTest.cs | 32 ++++++++++++++ .../Tests/Tests.csproj | 9 ++-- 12 files changed, 106 insertions(+), 31 deletions(-) create mode 100644 files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/PathExistsUsage.cs create mode 100644 files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithPathExistsIntegrationTest.cs diff --git a/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp.csproj b/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp.csproj index f02677bf64..dfb40caafc 100644 --- a/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp.csproj +++ b/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net10.0 enable enable diff --git a/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileAndDirectoryUsage.cs b/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileAndDirectoryUsage.cs index fac75f820a..4f5792d62d 100644 --- a/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileAndDirectoryUsage.cs +++ b/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileAndDirectoryUsage.cs @@ -11,7 +11,7 @@ public static void Run() // file var testFile = Path.Combine(Path.GetTempPath(), "test_file1.abc"); - File.CreateText(testFile); + File.WriteAllText(testFile, string.Empty); isFile = File.Exists(testFile); isDirectory = Directory.Exists(testFile); diff --git a/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileAttributesUsage.cs b/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileAttributesUsage.cs index 790cb9c72d..6c955ec3fc 100644 --- a/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileAttributesUsage.cs +++ b/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileAttributesUsage.cs @@ -1,6 +1,6 @@ namespace CheckPathFileOrDirectoryCSharp { - public class FileAttributesUsage + public static class FileAttributesUsage { public static void Run() { @@ -11,7 +11,7 @@ public static void Run() // file var testFile = Path.Combine(Path.GetTempPath(), "test_file4.abc"); - File.CreateText(testFile); + File.WriteAllText(testFile, string.Empty); var attributes = File.GetAttributes(testFile); @@ -32,18 +32,19 @@ public static void Run() Console.WriteLine($"{testDirectory}: isFile = {isFile}, isDirectory = {isDirectory}\n"); // no file or directory - var notExistingPath = "someNotExistingPath4"; + var notExistingPath = Path.Combine(Path.GetTempPath(), "someNotExistingPath4"); - try + isDirectory = false; + isFile = false; + + if (Path.Exists(notExistingPath)) { attributes = File.GetAttributes(notExistingPath); + isDirectory = attributes.HasFlag(FileAttributes.Directory); isFile = !isDirectory; } - catch (FileNotFoundException) - { - isFile = isDirectory = false; - } + Console.WriteLine($"{notExistingPath}: isFile = {isFile}, isDirectory = {isDirectory}\n"); } } diff --git a/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileInfoAndDirectoryInfoDrawbacks.cs b/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileInfoAndDirectoryInfoDrawbacks.cs index cd7b00d143..9f9c3c14c0 100644 --- a/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileInfoAndDirectoryInfoDrawbacks.cs +++ b/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileInfoAndDirectoryInfoDrawbacks.cs @@ -1,12 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CheckPathFileOrDirectoryCSharp +namespace CheckPathFileOrDirectoryCSharp { - public class FileInfoAndDirectoryInfoDrawbacks + public static class FileInfoAndDirectoryInfoDrawbacks { public static void Run() { @@ -23,13 +17,18 @@ public static void Run() Console.WriteLine($"existsFileInfo = {existsFileInfo}"); Console.WriteLine($"existsFile = {existsFile}"); - File.CreateText(testFile); + File.WriteAllText(testFile, string.Empty); existsFileInfo = fileInfo.Exists; existsFile = File.Exists(testFile); Console.WriteLine($"existsFileInfo = {existsFileInfo}"); - Console.WriteLine($"existsFile = {existsFile}\n"); + Console.WriteLine($"existsFile = {existsFile}"); + + fileInfo.Refresh(); + existsFileInfo = fileInfo.Exists; + + Console.WriteLine($"existsFileInfo after Refresh() = {existsFileInfo}\n"); } } diff --git a/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileInfoAndDirectoryInfoUsage.cs b/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileInfoAndDirectoryInfoUsage.cs index db19023c08..5804142d3a 100644 --- a/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileInfoAndDirectoryInfoUsage.cs +++ b/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/FileInfoAndDirectoryInfoUsage.cs @@ -1,6 +1,6 @@ namespace CheckPathFileOrDirectoryCSharp { - public class FileInfoAndDirectoryInfoUsage + public static class FileInfoAndDirectoryInfoUsage { public static void Run() { @@ -14,7 +14,7 @@ public static void Run() // file var testFile = Path.Combine(Path.GetTempPath(), "test_file2.abc"); - File.CreateText(testFile); + File.WriteAllText(testFile, string.Empty); fileInfo = new FileInfo(testFile); directoryInfo = new DirectoryInfo(testFile); diff --git a/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/PathExistsUsage.cs b/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/PathExistsUsage.cs new file mode 100644 index 0000000000..34c1f2029e --- /dev/null +++ b/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/PathExistsUsage.cs @@ -0,0 +1,43 @@ +namespace CheckPathFileOrDirectoryCSharp +{ + public static class PathExistsUsage + { + public static void Run() + { + Console.WriteLine("Using Path.Exists:"); + + bool anythingHere; + bool isDirectory; + bool isFile; + + // file + var testFile = Path.Combine(Path.GetTempPath(), "test_file5.abc"); + File.WriteAllText(testFile, string.Empty); + + anythingHere = Path.Exists(testFile); + isDirectory = Directory.Exists(testFile); + isFile = File.Exists(testFile); + + Console.WriteLine($"{testFile}: anythingHere = {anythingHere}, isDirectory = {isDirectory}, isFile = {isFile}\n"); + + // directory + var testDirectory = Path.Combine(Path.GetTempPath(), "test_directory5"); + Directory.CreateDirectory(testDirectory); + + anythingHere = Path.Exists(testDirectory); + isDirectory = Directory.Exists(testDirectory); + isFile = File.Exists(testDirectory); + + Console.WriteLine($"{testDirectory}: anythingHere = {anythingHere}, isDirectory = {isDirectory}, isFile = {isFile}\n"); + + // no file or directory + var notExistingPath = Path.Combine(Path.GetTempPath(), "someNotExistingPath5"); + + anythingHere = Path.Exists(notExistingPath); + isDirectory = Directory.Exists(notExistingPath); + isFile = File.Exists(notExistingPath); + + Console.WriteLine($"{notExistingPath}: anythingHere = {anythingHere}, isDirectory = {isDirectory}, isFile = {isFile}\n"); + } + } +} diff --git a/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/Program.cs b/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/Program.cs index 7e3d3a2190..d8cd70e016 100644 --- a/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/Program.cs +++ b/files-csharp/CheckPathFileOrDirectoryCSharp/CheckPathFileOrDirectoryCSharp/Program.cs @@ -3,4 +3,5 @@ FileAndDirectoryUsage.Run(); FileInfoAndDirectoryInfoUsage.Run(); FileInfoAndDirectoryInfoDrawbacks.Run(); +PathExistsUsage.Run(); FileAttributesUsage.Run(); diff --git a/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithAttributesIntegrationTest.cs b/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithAttributesIntegrationTest.cs index ec50f7e65f..fcad5b333f 100644 --- a/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithAttributesIntegrationTest.cs +++ b/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithAttributesIntegrationTest.cs @@ -17,7 +17,7 @@ public void WhenPathIsDirectory_ThenHasDirectoryAttribute() public void WhenPathIsFile_ThenHasNotDirectoryAttribute() { string path = Path.Combine(Path.GetTempPath(), "test_file_2.abc"); - File.CreateText(path); + File.WriteAllText(path, string.Empty); var attributes = File.GetAttributes(path); Assert.IsFalse(attributes.HasFlag(FileAttributes.Directory)); @@ -28,7 +28,7 @@ public void WhenPathNotExists_ThenThrowsException() { string path = Path.Combine(Path.GetTempPath(), "test_not_existing_2.abc"); - Assert.ThrowsException(() => { var attributes = File.GetAttributes(path); }); + Assert.ThrowsExactly(() => { var attributes = File.GetAttributes(path); }); } } } diff --git a/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithFileAndDirectoryIntegrationTest.cs b/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithFileAndDirectoryIntegrationTest.cs index 479ad43208..14f23b4486 100644 --- a/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithFileAndDirectoryIntegrationTest.cs +++ b/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithFileAndDirectoryIntegrationTest.cs @@ -17,7 +17,7 @@ public void WhenPathIsDirectory_ThenDirectoryExistsIsTrue() public void WhenPathIsFile_ThenFileExistsIsTrue() { string path = Path.Combine(Path.GetTempPath(), "test_file_1.abc"); - File.CreateText(path); + File.WriteAllText(path, string.Empty); Assert.IsFalse(Directory.Exists(path)); Assert.IsTrue(File.Exists(path)); diff --git a/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithFileInfoAndDirectoryInfoIntegrationTest.cs b/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithFileInfoAndDirectoryInfoIntegrationTest.cs index f7be1c3595..4832b8d855 100644 --- a/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithFileInfoAndDirectoryInfoIntegrationTest.cs +++ b/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithFileInfoAndDirectoryInfoIntegrationTest.cs @@ -20,7 +20,7 @@ public void WhenPathIsDirectory_ThenDirectoryExistsIsTrue() public void WhenPathIsFile_ThenFileExistsIsTrue() { string path = Path.Combine(Path.GetTempPath(), "test_file_3.abc"); - File.CreateText(path); + File.WriteAllText(path, string.Empty); var fileInfo = new FileInfo(path); var directoryInfo = new DirectoryInfo(path); diff --git a/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithPathExistsIntegrationTest.cs b/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithPathExistsIntegrationTest.cs new file mode 100644 index 0000000000..68decab422 --- /dev/null +++ b/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/PathTypeWithPathExistsIntegrationTest.cs @@ -0,0 +1,32 @@ +namespace Tests +{ + [TestClass] + public class PathTypeWithPathExistsIntegrationTest + { + [TestMethod] + public void WhenPathIsDirectory_ThenPathExistsIsTrue() + { + string path = Path.Combine(Path.GetTempPath(), "test_directory_4"); + Directory.CreateDirectory(path); + + Assert.IsTrue(Path.Exists(path)); + } + + [TestMethod] + public void WhenPathIsFile_ThenPathExistsIsTrue() + { + string path = Path.Combine(Path.GetTempPath(), "test_file_4.abc"); + File.WriteAllText(path, string.Empty); + + Assert.IsTrue(Path.Exists(path)); + } + + [TestMethod] + public void WhenPathNotExists_ThenPathExistsIsFalse() + { + string path = Path.Combine(Path.GetTempPath(), "test_not_existing_4.abc"); + + Assert.IsFalse(Path.Exists(path)); + } + } +} diff --git a/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/Tests.csproj b/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/Tests.csproj index 0445fb749d..ac4def59c8 100644 --- a/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/Tests.csproj +++ b/files-csharp/CheckPathFileOrDirectoryCSharp/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable @@ -10,10 +10,9 @@ - - - - + + +