Skip to content

FileStream Write has no effect when the file is deleted #63208

@skyoxZ

Description

@skyoxZ

Description

Not sure if it's by design. FileStream Write has no effect when the file is deleted.

Reproduction Steps

using System.IO;

namespace FileStreamTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = "a.txt";
            FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.Delete);
            File.Delete(filePath);
            fs.WriteByte(65);
            fs.Flush();
        }
    }
}

Expected behavior

Throw an exception.

Actual behavior

Not throw.

Regression?

I checked .NET 4.8 and it has the same behavior.

Known Workarounds

No response

Configuration

.NET 6.0.101
Windows 11 x64, 21H2

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.IOquestionAnswer questions and provide assistance, not an issue with source code or documentation.untriagedNew issue has not been triaged by the area owner

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions