{"record":{"id":"d11479f5fd3c670b","repo":"dotnet/wpf","slug":"sr-storagealreadyexist","errorCode":null,"errorMessage":"SR.StorageAlreadyExist","messagePattern":"SR\\.StorageAlreadyExist","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageInfo.cs","lineNumber":674,"sourceCode":"                            SR.Format(SR.NamedAPIFailure, \"IStorage.CreateStorage\"), \n                            nativeCallErrorCode ));\n                }\n                else\n                {\n                    throw new IOException(\n                        SR.UnableToCreateStorage,\n                        new COMException( \n                            SR.Format(SR.NamedAPIFailure, \"IStorage.CreateStorage\"), \n                            nativeCallErrorCode ));\n                }\n            }\n    \n            // Invalidate enumerators\n            InvalidateEnumerators();\n        }\n        else\n        {\n            throw new IOException(SR.StorageAlreadyExist);\n        }\n    \n        // Return a reference\n        return newSubStorage;\n    }\n    \n    /// <summary>\n    /// Deletes a storage, recursively if specified.\n    /// </summary>\n    /// <param name=\"recursive\">Whether to recursive delete all existing content</param>\n    /// <param name=\"name\">Name of storage</param>\n    internal bool Delete( bool recursive , string name)\n    {\n        bool storageDeleted = false;\n        CheckDisposedStatus();\n        if( null == parentStorage )\n        {\n            // We are the root storage, you can't \"delete\" the root storage!","sourceCodeStart":656,"sourceCodeEnd":692,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageInfo.cs#L656-L692","documentation":"Thrown by StorageInfo.CreateStorage when a sub-storage with the requested name already exists in this storage and the creation would overwrite it. The compound-file API surfaces this as IOException(SR.StorageAlreadyExist) instead of silently replacing the existing subtree.","triggerScenarios":"Calling CreateStorage/CreateSubStorage with a name for which StorageExists(name) is already true; re-running an idempotent initialization routine that always creates the same named storages.","commonSituations":"Re-opening and re-initializing an XPS package that already contains '\\u0006DataSpaces' or 'Fragments' storages; retry logic that recreates the same names without checking existence first.","solutions":["Check StorageExists(name) before creating and reuse the existing storage if present","Delete the existing storage first via Delete(recursive: true) if replacement is intended","Use a unique name (e.g. include a GUID) when storages must be distinct","Make initialization idempotent by fetching-or-creating rather than always creating"],"exampleFix":"// before\nroot.CreateSubStorage(\"Fragments\");\n// after\nvar fragments = root.StorageExists(\"Fragments\")\n    ? root.GetSubStorageInfo(\"Fragments\")\n    : root.CreateSubStorage(\"Fragments\");","handlingStrategy":"validation","validationCode":"if (storage.StorageExists(name)) return storage.GetSubStorageInfo(name); // reuse","typeGuard":"StorageInfo GetOrCreate(StorageInfo s, string name) => s.StorageExists(name) ? s.GetSubStorageInfo(name) : s.CreateSubStorage(name);","tryCatchPattern":"try { storage.CreateSubStorage(name); }\ncatch (IOException ex) when (ex.Message.Contains(\"exist\")) { /* reuse existing */ }","preventionTips":["Always check StorageExists before CreateStorage","Use get-or-create helpers instead of blind creation","Delete-then-recreate only when replacement is intentional"],"tags":["compound-file","storage","already-exists","packaging"],"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-22T01:17:13.364Z"}