Skip to content

Add DirectoryPath property to DirectoryNotFoundException#126428

Merged
ViveliDuCh merged 7 commits intodotnet:mainfrom
ViveliDuCh:dev/vivianad/directorynotfound-directorypath
Apr 6, 2026
Merged

Add DirectoryPath property to DirectoryNotFoundException#126428
ViveliDuCh merged 7 commits intodotnet:mainfrom
ViveliDuCh:dev/vivianad/directorynotfound-directorypath

Conversation

@ViveliDuCh
Copy link
Copy Markdown
Member

@ViveliDuCh ViveliDuCh commented Apr 1, 2026

Closes #103468

Adds a DirectoryPath property to DirectoryNotFoundException as approved in API review.

What's included

  • DirectoryPath nullable property on DirectoryNotFoundException, matching the pattern of FileNotFoundException.FileName
  • Two new constructors accepting directoryPath (message + directoryPath, message + directoryPath + innerException)
  • Auto-constructed message when message is null but directoryPath is provided, via null-coalescing in the constructors
  • ToString() override that includes DirectoryPath on a separate line
  • Serialization support via GetObjectData / version-tolerant deserialization constructor
  • Ref assembly updates for System.Runtime
  • Updated Arg_DirectoryNotFoundException default message from "Attempted to access a path that is not on the disk." to "Unable to find the specified directory." to match FileNotFoundException's "Unable to find the specified file." pattern and align better with the new IO_DirectoryNotFound_Path string.
  • Tests for all new constructors, property, message auto-construction, ToString() output, null edge cases, and serialization round-trip

Implementation notes

  • Follows the FileNotFoundException implementation pattern:
    • The property is string? (nullable) because not all throw sites have a path available
    • The new constructors null-coalesce message (consistent with existing DNFE constructors), auto-constructing a descriptive message from directoryPath when message is null
  • Uses managed SR.Format(...) for message construction rather than native interop, as recommended during API review
  • Version-tolerant deserialization: iterates SerializationInfo entries to handle old payloads that lack the DirectoryPath field
  • #if NET11_0_OR_GREATER guards on throw sites in multi-TFM files (Win32Marshal.cs, Interop.IOErrors.cs, PhysicalFileProvider.cs, LdapSessionOptions.Linux.cs) since the new constructor isn't available on older TFM targets

Internal throw-site updates

Updated 18 internal throw sites across 12 files to pass directory paths to the new constructors, so that DirectoryPath is populated for runtime-generated exceptions:

Area File(s) Path variable
CoreLib / Common Win32Marshal.cs path
CoreLib / Common Interop.IOErrors.cs (Unix ENOTDIR) path
CoreLib FileInfo.cs (MoveTo) directory of FullName
CoreLib FileSystem.Unix.cs (×2, directory rename) sourceFullPath
System.Formats.Tar TarFile.cs (×8) sourceDirectoryName / destinationDirectoryName
System.IO.IsolatedStorage IsolatedStorageFile.cs sourceDirectoryName
System.IO.FileSystem.AccessControl FileSystemSecurity.cs name
System.Net.Sockets SocketAsyncEventArgs.Unix.cs dirname
System.Security.AccessControl NativeObjectSecurity.cs name
System.DirectoryServices.Protocols LdapSessionOptions.Linux.cs value
Microsoft.Extensions.FileProviders.Physical PhysicalFileProvider.cs Root

Not updated (by design):

  • 3 sites with no path available (SharedMemoryManager.Unix.cs default ctor, Interop.IOErrors.cs empty-path branch, FileSystemSecurity.cs null-name branch)
  • 4 threading sites (EventWaitHandle, Mutex, EventWaitHandleAcl, MutexAcl) where the name is a named sync object, not a filesystem directory

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a DirectoryPath property and related API surface to System.IO.DirectoryNotFoundException to enable machine-readable access to the missing directory path (similar to FileNotFoundException.FileName).

Changes:

  • Added DirectoryPath property and new constructor overloads accepting directoryPath.
  • Implemented Message and ToString() overrides to incorporate DirectoryPath (including auto-message when message is null).
  • Added serialization support and updated reference assembly and tests.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.Private.CoreLib/src/System/IO/DirectoryNotFoundException.cs Implements DirectoryPath property, new constructors, Message/ToString() overrides, and serialization plumbing.
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx Adds new localized strings used for message/to-string formatting.
src/libraries/System.Runtime/ref/System.Runtime.cs Updates ref assembly to include the new constructors, property, and overrides.
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/IO/DirectoryNotFoundExceptionTests.cs Adds unit tests covering the new API surface and formatting behaviors.

@ViveliDuCh ViveliDuCh force-pushed the dev/vivianad/directorynotfound-directorypath branch from cc6f9cb to c4ad204 Compare April 1, 2026 19:10
@jkotas
Copy link
Copy Markdown
Member

jkotas commented Apr 1, 2026

Optional follow-up

When we introduce new APIs that can be used within the BCL, we typically add their use within the BCL as part of same PR to validate that the API actually works well in practice.

Copilot AI review requested due to automatic review settings April 1, 2026 23:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated 2 comments.

Copilot AI review requested due to automatic review settings April 2, 2026 20:51
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated 4 comments.

…alization and null-coalesce message in ctors/drop Message override.
Copilot AI review requested due to automatic review settings April 3, 2026 15:07
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated no new comments.

Copy link
Copy Markdown
Member

@jozkee jozkee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise; LGTM, thanks.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated 1 comment.

@ViveliDuCh ViveliDuCh force-pushed the dev/vivianad/directorynotfound-directorypath branch from 3974814 to a27f22c Compare April 3, 2026 23:15
Copilot AI review requested due to automatic review settings April 4, 2026 07:32
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated 1 comment.

@ViveliDuCh
Copy link
Copy Markdown
Member Author

/ba-g Build Analysis stuck idle (2+ days, no re-run option). Failure is MacCatalyst infra issue: XHarness exit 82 (RETURN_CODE_NOT_SET), test runner couldn't read app exit code (empty log). Previous run passed.

@ViveliDuCh ViveliDuCh merged commit c131725 into dotnet:main Apr 6, 2026
154 of 157 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a DirectoryPath property to DirectoryNotFoundException to be consistent with FileNotFoundException

5 participants