{"record":{"id":"b3f2508b93cf0ca4","repo":"dotnet/wpf","slug":"sr-ioexceptionwithfilename","errorCode":null,"errorMessage":"SR.IOExceptionWithFileName","messagePattern":"SR\\.IOExceptionWithFileName","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs","lineNumber":760,"sourceCode":"        /// <param name=\"fileName\">File name string.</param>\n        internal static void ThrowWin32Exception(int errorCode, string fileName)\n        {\n            switch (errorCode)\n            {\n                case NativeMethods.ERROR_FILE_NOT_FOUND:\n                    throw new FileNotFoundException(SR.Format(SR.FileNotFoundExceptionWithFileName, fileName), fileName);\n\n                case NativeMethods.ERROR_PATH_NOT_FOUND:\n                    throw new DirectoryNotFoundException(SR.Format(SR.DirectoryNotFoundExceptionWithFileName, fileName));\n\n                case NativeMethods.ERROR_ACCESS_DENIED:\n                    throw new UnauthorizedAccessException(SR.Format(SR.UnauthorizedAccessExceptionWithFileName, fileName));\n\n                case NativeMethods.ERROR_FILENAME_EXCED_RANGE:\n                    throw new PathTooLongException(SR.Format(SR.PathTooLongExceptionWithFileName, fileName));\n\n                default:\n                    throw new IOException(SR.Format(SR.IOExceptionWithFileName, fileName), NativeMethods.MakeHRFromErrorCode(errorCode));\n            }\n        }\n\n        internal static Exception ConvertInPageException(FontSource fontSource, SEHException e)\n        {\n            string fileName;\n            if (fontSource.IsFile)\n            {\n                fileName = fontSource.Uri.LocalPath;\n            }\n            else\n            {\n                fileName = fontSource.GetUriString();\n            }\n\n            return new IOException(SR.Format(SR.IOExceptionWithFileName, fileName), e);\n        }\n    }","sourceCodeStart":742,"sourceCodeEnd":778,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs#L742-L778","documentation":"The default branch of Util.ThrowWin32Exception wraps any unrecognized Win32 error into IOException with SR.IOExceptionWithFileName and an HRESULT made from the error code, for font-file operations that fail for reasons not covered by the specific cases.","triggerScenarios":"A font file operation (open/create-file-mapping path in FontSource) returning a Win32 error other than 2, 3, 5, or 206 — e.g. sharing violation (32), disk error, or file in use — routed to the default case.","commonSituations":"Font file locked by another process (font viewer/installer), sharing violations, disk/media errors, file on unavailable removable media.","solutions":["Check which process locks the font file (e.g. Sysinternals handle.exe) and release/close it.","Retry the operation after the lock is released.","Copy the font to an app-owned location and load from there.","Inspect the HResult/inner Win32 error to identify the specific failure."],"exampleFix":"// before\nvar gt = new GlyphTypeface(new Uri(path)); // IOException: file locked by font viewer\n// after\ntry { var gt = new GlyphTypeface(new Uri(path)); }\ncatch (IOException ex) {\n    // fall back to in-memory copy so the file need not stay open\n    byte[] bytes = File.ReadAllBytes(path);\n    var gt = new GlyphTypeface(new MemoryStream(bytes).AsRandomAccessStreamLikeWrapper());\n}","handlingStrategy":"try-catch","validationCode":"try { using var fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read); }\ncatch (IOException) { /* file locked or unreadable — copy or retry later */ }","typeGuard":null,"tryCatchPattern":"try\n{\n    var gt = new GlyphTypeface(fontUri);\n}\ncatch (IOException ex)\n{\n    Log($\"Font I/O failed: {ex.Message}\");\n    // inspect ex.HResult for the underlying Win32 error; retry or fall back\n}","preventionTips":["Do not hold font files open in other processes (font viewers, installers).","Copy fonts to app-local storage before loading.","Open font files with FileShare.Read to reduce sharing violations."],"tags":["io","font","win32","file-locked","wpf"],"backgroundTag":"file-read-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"}