Skip to content

[API Proposal]: File.WriteAll bad path names. #117070

Closed as not planned
Closed as not planned
@MiLO83

Description

@MiLO83

Background and motivation

Title: File.WriteAllText / File.WriteAllBytes accept unsupported path characters in filename input
Body:
Hi .NET Team,
I've encountered a behavior in both File.WriteAllText and File.WriteAllBytes where they accept strings for the path parameter that contain characters not supported in directory structures (e.g., :, *, ?, <, >). While the underlying system eventually throws an exception due to invalid paths, the initial method call does not validate or proactively reject these characters—potentially leading to confusing errors downstream or platform-specific behavior.
Example:
File.WriteAllText("invalid:filename.txt", "test content");
// No validation at this point – OS handles error during file operation.

Expected Behavior:
These APIs should:

  • Either validate the provided filename string for unsupported characters and throw a meaningful exception early, or
  • Document clearly that the path string is not validated and platform constraints apply.
    This small change in behavior or documentation could greatly improve developer clarity and prevent runtime surprises—especially in cross-platform or automation-heavy workflows.
    Thanks for all your work maintaining such a powerful platform.
    Best,
    Myles Cameron Johnston

Let me know if you’d like to include platform info or suggest an implementation detail—I’m happy to refine it with you.

API Proposal

namespace System.Collections.Generic;

public class MyFancyCollection<T> : IEnumerable<T>
{
    public void Fancy(T item);
}

API Usage

// Fancy the value
var c = new MyFancyCollection<int>();
c.Fancy(42);

// Getting the values out
foreach (var v in c)
    Console.WriteLine(v);

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions