Skip to content

Add File.TryDelete and Directory.TryDelete returning bool#20

Merged
Tyrrrz merged 6 commits intoprimefrom
copilot/add-file-trydelete-directory-trydelete
Apr 14, 2026
Merged

Add File.TryDelete and Directory.TryDelete returning bool#20
Tyrrrz merged 6 commits intoprimefrom
copilot/add-file-trydelete-directory-trydelete

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 14, 2026

Adds TryDelete extension methods on File and Directory that swallow exceptions and return a bool indicating success.

Changes

  • FileExtensionsFile.TryDelete(string path) → bool: wraps File.Delete; returns true even for non-existent paths (consistent with BCL behavior where File.Delete is a no-op on missing files)
  • DirectoryExtensions (new file) — Directory.TryDelete(string path, bool recursive = false) → bool: wraps Directory.Delete; returns false for non-existent directories or non-empty directories when recursive is false

Usage

// Returns true (file deleted)
File.TryDelete("/tmp/some-file.txt");

// Returns true (BCL File.Delete is a no-op for missing files)
File.TryDelete("/tmp/nonexistent.txt");

// Returns false (directory not empty, recursive not set)
Directory.TryDelete("/tmp/some-dir");

// Returns true (recursively deleted)
Directory.TryDelete("/tmp/some-dir", recursive: true);

Copilot AI and others added 2 commits April 12, 2026 18:48
@Tyrrrz Tyrrrz marked this pull request as ready for review April 14, 2026 15:07
Copilot AI review requested due to automatic review settings April 14, 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

Adds TryDelete extension methods to simplify deleting files/directories by swallowing exceptions and returning a bool success indicator.

Changes:

  • Add File.TryDelete(string) -> bool wrapper around File.Delete.
  • Add Directory.TryDelete(string, bool recursive = false) -> bool wrapper around Directory.Delete.
  • Add unit tests covering success, non-existent targets, and recursive/non-recursive directory deletion behavior.

Reviewed changes

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

File Description
PowerKit/Extensions/FileExtensions.cs Adds File.TryDelete extension returning bool.
PowerKit/Extensions/DirectoryExtensions.cs Introduces Directory.TryDelete extension returning bool.
PowerKit.Tests/FileExtensionsTests.cs Adds tests for File.TryDelete (existing + missing file).
PowerKit.Tests/DirectoryExtensionsTests.cs Adds tests for Directory.TryDelete (existing, missing, non-empty, recursive).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread PowerKit/Extensions/FileExtensions.cs
Comment thread PowerKit/Extensions/FileExtensions.cs Outdated
Comment thread PowerKit/Extensions/DirectoryExtensions.cs
Comment thread PowerKit.Tests/DirectoryExtensionsTests.cs Outdated
Tyrrrz and others added 2 commits April 14, 2026 18:13
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants