Skip to content

Avoid unnecessary DirectoryInfo allocation in FileInfo.MoveTo#123893

Merged
stephentoub merged 1 commit intodotnet:mainfrom
stephentoub:movetoalloc
Feb 2, 2026
Merged

Avoid unnecessary DirectoryInfo allocation in FileInfo.MoveTo#123893
stephentoub merged 1 commit intodotnet:mainfrom
stephentoub:movetoalloc

Conversation

@stephentoub
Copy link
Member

No description provided.

@dotnet-policy-service
Copy link
Contributor

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

Copy link
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

This PR optimizes the FileInfo.MoveTo method by replacing an unnecessary DirectoryInfo allocation with a direct call to Directory.Exists, improving performance while maintaining equivalent functionality.

Changes:

  • Replaced new DirectoryInfo(Path.GetDirectoryName(FullName)!).Exists with Directory.Exists(Path.GetDirectoryName(FullName)) to avoid allocating a DirectoryInfo object
  • Removed the null-forgiving operator (!) as Directory.Exists properly handles null input

@stephentoub stephentoub enabled auto-merge (squash) February 2, 2026 19:51
@stephentoub stephentoub merged commit 2c6a03f into dotnet:main Feb 2, 2026
155 of 156 checks passed
@stephentoub stephentoub deleted the movetoalloc branch February 4, 2026 03:47
lewing pushed a commit to lewing/runtime that referenced this pull request Feb 9, 2026
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.

3 participants