{"record":{"id":"d5f9d9ef6fd29c76","repo":"dotnet/wpf","slug":"sr-filealreadyexists","errorCode":null,"errorMessage":"SR.FileAlreadyExists","messagePattern":"SR\\.FileAlreadyExists","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs","lineNumber":255,"sourceCode":"\n        IStorage newRootStorage;\n\n        ////////////////////////////////////////////////////////////////////\n        // Generate STGM from FileMode\n        switch(mode)\n        {\n            case FileMode.Append:\n                throw new ArgumentException(\n                    SR.FileModeUnsupported);\n            case FileMode.Create:\n                grfMode |= SafeNativeCompoundFileConstants.STGM_CREATE;\n                break;\n            case FileMode.CreateNew:\n                {\n                    FileInfo existTest = new FileInfo(path);\n                    if( existTest.Exists )\n                    {\n                        throw new IOException(\n                            SR.FileAlreadyExists);\n                    }\n                }\n                goto case FileMode.Create;\n            case FileMode.Open:\n                break;\n            case FileMode.OpenOrCreate:\n                {\n                    FileInfo existTest = new FileInfo(path);\n                    if( existTest.Exists )\n                    {\n                        // File exists, use open code path\n                        goto case FileMode.Open;\n                    }\n                    else\n                    {\n                        // File does not exist, use create code path\n                        goto case FileMode.Create;","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageRoot.cs#L237-L273","documentation":"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.","triggerScenarios":"Calling StorageRoot.Open / Package.Open with FileMode.CreateNew when a file already exists at the given path.","commonSituations":"Re-running a generator that creates a package without deleting or versioning the output file; race between two processes creating the same container.","solutions":["Delete or rename the existing file before opening with CreateNew.","Use FileMode.Create (overwrite) or OpenOrCreate instead if replacing is acceptable.","Check File.Exists(path) before choosing CreateNew.","Handle the IOException to fall back to opening the existing package."],"exampleFix":"// before\nPackage.Open(path, FileMode.CreateNew, FileAccess.Write); // throws if exists\n// after\nvar mode = File.Exists(path) ? FileMode.Create : FileMode.CreateNew;\nPackage.Open(path, mode, FileAccess.Write);","handlingStrategy":"validation","validationCode":"if (mode == FileMode.CreateNew && File.Exists(path))\n    throw new IOException($\"File already exists: {path}\");","typeGuard":"bool CanCreateNew(string path) => !File.Exists(path);","tryCatchPattern":"try { pkg = Package.Open(path, FileMode.CreateNew, access); }\ncatch (IOException) { pkg = Package.Open(path, FileMode.Open, access); }","preventionTips":["Check File.Exists before CreateNew.","Prefer FileMode.Create when overwrite is acceptable.","Use unique/temp file names for generated packages."],"tags":["io","filemode","file-exists"],"backgroundTag":"file-already-exists","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}