dotnet/wpf · error · IOException

SR.FileAlreadyExists

Error message

SR.FileAlreadyExists

What it means

FileMode.CreateNew requires that the file not exist; StorageRoot.Open checks the path first and throws IOException if a file is already there. This mirrors normal FileStream CreateNew semantics applied to compound file containers.

Solutions

  1. Delete or rename the existing file before opening with CreateNew.
  2. Use FileMode.Create (overwrite) or OpenOrCreate instead if replacing is acceptable.
  3. Check File.Exists(path) before choosing CreateNew.
  4. Handle the IOException to fall back to opening the existing package.

Example fix

// before
Package.Open(path, FileMode.CreateNew, FileAccess.Write); // throws if exists
// after
var mode = File.Exists(path) ? FileMode.Create : FileMode.CreateNew;
Package.Open(path, mode, FileAccess.Write);
Defensive patterns

Strategy: validation

Validate before calling

if (mode == FileMode.CreateNew && File.Exists(path))
    throw new IOException($"File already exists: {path}");

Type guard

bool CanCreateNew(string path) => !File.Exists(path);

Try / catch

try { pkg = Package.Open(path, FileMode.CreateNew, access); }
catch (IOException) { pkg = Package.Open(path, FileMode.Open, access); }

Prevention

When it happens

Trigger: Calling StorageRoot.Open / Package.Open with FileMode.CreateNew when a file already exists at the given path.

Common situations: Re-running a generator that creates a package without deleting or versioning the output file; race between two processes creating the same container.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/d5f9d9ef6fd29c76. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs:255

        IStorage newRootStorage;

        ////////////////////////////////////////////////////////////////////
        // Generate STGM from FileMode
        switch(mode)
        {
            case FileMode.Append:
                throw new ArgumentException(
                    SR.FileModeUnsupported);
            case FileMode.Create:
                grfMode |= SafeNativeCompoundFileConstants.STGM_CREATE;
                break;
            case FileMode.CreateNew:
                {
                    FileInfo existTest = new FileInfo(path);
                    if( existTest.Exists )
                    {
                        throw new IOException(
                            SR.FileAlreadyExists);
                    }
                }
                goto case FileMode.Create;
            case FileMode.Open:
                break;
            case FileMode.OpenOrCreate:
                {
                    FileInfo existTest = new FileInfo(path);
                    if( existTest.Exists )
                    {
                        // File exists, use open code path
                        goto case FileMode.Open;
                    }
                    else
                    {
                        // File does not exist, use create code path
                        goto case FileMode.Create;

View on GitHub (pinned to 81131a70a4)