[API Proposal]: Add File.Delete and Directory.Delete overloads that specify not found throw behavior

Open
#117,853 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Stale
Tech stack
csharp

Research direction

Start by reviewing the linked File.Delete implementation in src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.Windows.cs, then compare the existing File.Delete and Directory.Delete behavior described in the proposal. The work is done when the API design is accepted and the requested not-found behavior is consistently defined for both overloads.

Written by the indexing model from the issue text.

Description

api-suggestion area-System.IO
Background and motivation

Currently, File.Delete() never throws on file not found (it swallows the error), and Directory.Delete() always throws if the directory is not found. The former limitation means we need to jump hoops to ensure correctness in a transactional file storage engine and make additional syscalls which impacts perf, and the latter is a perf issue (need to try/catch just to discard the unnecessary exception).

API Proposal

What I would like:

public static class File
{
    public static void Delete(string path, bool throwNotFound);
}

public static class Directory
{
    public static void Delete(string path, bool recursive, bool throwNotFound);
}

That way I can specify whether I want to be notified if the dir/file does not exist.

API Usage
File.Delete("some/path", true); // throws if "some/path" does not exist
Directory.Delete("some/path", true, false); // does not throw if "some/path" does not exist
Alternative Designs
public static class File
{
    public static void Delete(string path, bool ignoreNotFound);
}

public static class Directory
{
    public static void Delete(string path, bool recursive, bool ignoreNotFound);
}
Risks

None

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.