Skip to content

Check if a path is a file or a directory: retarget net10.0, add Path.Exists, guard File.GetAttributes - #2229

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/103825-csharp-path-file-or-directory
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/103825-csharp-path-file-or-directory

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample update for the article "Check if a Path Exists and Is a File or a Directory in C#" (https://code-maze.com/csharp-path-file-or-directory/). Touches only files-csharp/CheckPathFileOrDirectoryCSharp.

Changes:

  • Both projects retargeted from net7.0 to net10.0.
  • FileAttributesUsage: the try/catch that caught only FileNotFoundException is replaced by a Path.Exists() guard. File.GetAttributes() throws DirectoryNotFoundException when a parent directory of the path is missing, and the old catch let that through.
  • File.CreateText(path) replaced by File.WriteAllText(path, string.Empty) in seven files, so the returned StreamWriter is no longer left open.
  • FileInfoAndDirectoryInfoDrawbacks now calls Refresh() and prints the fresh answer after the stale one.
  • New PathExistsUsage class (called from Program.cs) and a new PathTypeWithPathExistsIntegrationTest with three tests: true for a file, true for a directory, false for a missing path.
  • MSTest 4 rename: Assert.ThrowsException becomes Assert.ThrowsExactly at the one call site.
  • Five unused usings removed from FileInfoAndDirectoryInfoDrawbacks; FileAttributesUsage, FileInfoAndDirectoryInfoUsage and FileInfoAndDirectoryInfoDrawbacks made static.

Packages, re-queried from api.nuget.org on 2026-09-23 (newest stable, all listed):

  • Microsoft.NET.Test.Sdk 17.7.1 -> 18.10.1 (the batch plan said 18.10.0; the re-query wins)
  • MSTest.TestAdapter 2.2.10 + MSTest.TestFramework 2.2.10 -> MSTest 4.4.1 meta-package (the batch plan said 4.4.0; the re-query wins)
  • coverlet.collector 3.2.0 -> 10.0.1

Verified locally on SDK 10.0.302, runtime .NET 10.0.10: dotnet build -c Release 0 warnings, 0 errors; dotnet test 12 passed, 0 failed (9 existing + 3 new); dotnet list package --vulnerable --include-transitive reports no vulnerable packages on either project.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant