{"record":{"id":"d7a0784eb33ff0d3","repo":"dotnet/wpf","slug":"sr-writefailure","errorCode":null,"errorMessage":"SR.WriteFailure","messagePattern":"SR\\.WriteFailure","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CFStream.cs","lineNumber":268,"sourceCode":"\n        PackagingUtilities.VerifyStreamWriteArgs(this, buffer, offset, count);\n\n        int written = 0; // CLR guys have deemed this uninteresting?\n\n        if ( 0 == offset ) // Zero offset is typical case\n        {\n            _safeIStream.Write( buffer, count, out written );\n        }\n        else // Non-zero offset\n        {\n            // Copy from indicated offset to zero-based temp buffer\n            byte[] localBuffer = new byte[count];\n            Array.Copy(buffer, offset, localBuffer, 0, count);\n            _safeIStream.Write( localBuffer, count, out written );\n        }\n\n        if( count != written )\n            throw new IOException(\n                SR.WriteFailure);\n}\n\n    //------------------------------------------------------\n    //\n    //  Internal Methods\n    //\n    //------------------------------------------------------\n\n    // Check whether this Stream object is still valid.  If not, thrown an\n    //  ObjectDisposedException.\n    internal void CheckDisposedStatus()\n    {\n        if( StreamDisposed )\n            throw new ObjectDisposedException(null, SR.StreamObjectDisposed);\n    }\n\n    // Check whether this Stream object is still valid.","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompoundFile/CFStream.cs#L250-L286","documentation":"CFStream.Write throws IOException with SR.WriteFailure when the underlying compound-file native IStream.Write reports a byte count different from the requested count. A partial write inside a compound file means the storage engine could not commit all bytes, so the stream surface treats it as a hard I/O failure rather than silently accepting a short write.","triggerScenarios":"Calling Write when the compound file is full, the disk is out of space, the file is locked/corrupted, or the native StgWrite fails partway and reports written != count.","commonSituations":"Disk-quota or free-space exhaustion while saving large documents; writes to a compound file on a flaky network share or removable media; corrupted OLE container files that fail mid-write.","solutions":["Check available disk space and free space / quota before writing","Verify the compound file is not corrupt (try opening it with a repair tool) and the media is writable","Close and reopen the file, then retry the save operation","Copy content to a new file if the original container is damaged","Catch IOException around writes and implement save-to-temp-then-replace logic"],"exampleFix":"// before\ncfStream.Write(data, 0, data.Length);\n// after\ntry {\n    cfStream.Write(data, 0, data.Length);\n} catch (IOException ex) {\n    throw new IOException(\"Compound file write failed (disk full or corrupt container?): \" + ex.Message, ex);\n}","handlingStrategy":"try-catch","validationCode":"// before writing\nlong free = new DriveInfo(Path.GetPathRoot(path)).AvailableFreeSpace;\nif (free < data.Length + margin) throw new IOException(\"Insufficient disk space.\");","typeGuard":null,"tryCatchPattern":"try { cfStream.Write(buffer, 0, count); } catch (IOException ex) { /* check disk space / file corruption, retry to temp file */ }","preventionTips":["Check disk free space / quota before large writes","Write to a temp file then atomically replace on save","Handle partial-write IOException as unrecoverable container damage","Avoid writing compound files directly on flaky network shares"],"tags":["io","write","compound-file","disk","wpf"],"backgroundTag":"file-write-failed","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"}