{"record":{"id":"8d16302831b92034","repo":"dotnet/wpf","slug":"sr-unauthorizedaccessexceptionwithfilename","errorCode":null,"errorMessage":"SR.UnauthorizedAccessExceptionWithFileName","messagePattern":"SR\\.UnauthorizedAccessExceptionWithFileName","errorType":"exception","errorClass":"UnauthorizedAccessException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs","lineNumber":754,"sourceCode":"        /// <summary>\n        /// This function performs job similar to CLR's internal __Error.WinIOError function:\n        /// it maps win32 errors from file I/O to CLR exceptions and includes string where possible.\n        /// However, we're interested only in errors when opening a file for reading.\n        /// </summary>\n        /// <param name=\"errorCode\">Win32 error code.</param>\n        /// <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            {","sourceCodeStart":736,"sourceCodeEnd":772,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/FontCache/FontCacheUtil.cs#L736-L772","documentation":"Resource-formatted PathTooLongException produced by FontCacheUtil.ThrowWin32Exception: when the Win32 error for a path exceeding MAX_PATH is returned while opening a font file for reading, it is translated into a PathTooLongException whose message includes the offending file name.","triggerScenarios":"Opening a font source where the OS denies access (Win32 error 5) — e.g. font in a protected directory (C:\\Windows\\Fonts opened without rights, Program Files, another user's profile).","commonSituations":"Running the app without elevation while the font file has restrictive ACLs; fonts installed per-user in another account's profile; corporate-locked font directories; antivirus/ACL restrictions.","solutions":["Grant the running user read access (ACLs) to the font file/directory.","Copy the font into an application-accessible location (app directory or app data) and reference it there.","Run under an account with sufficient permissions, or elevate if appropriate.","For system fonts, reference them via installed-font family APIs instead of opening the file directly."],"exampleFix":"// before\nvar gt = new GlyphTypeface(new Uri(@\"C:\\Windows\\Fonts\\Restricted.ttf\")); // access denied\n// after\nstring dest = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, \"Fonts\", \"MyFont.ttf\");\nvar gt = new GlyphTypeface(new Uri(dest)); // font shipped with app, readable","handlingStrategy":"try-catch","validationCode":"try { using var _ = File.OpenRead(fontUri.LocalPath); }\ncatch (UnauthorizedAccessException) { /* copy font to app dir or adjust ACLs first */ }","typeGuard":null,"tryCatchPattern":"try\n{\n    var gt = new GlyphTypeface(fontUri);\n}\ncatch (UnauthorizedAccessException)\n{\n    // fall back to an app-local copy of the font\n    var gt = new GlyphTypeface(new Uri(appLocalFontPath));\n}","preventionTips":["Ship fonts inside the app package instead of reading protected system folders.","Grant read ACLs to the app identity on any shared font directories.","Never require elevation for normal font loading."],"tags":["access-denied","permissions","font","io","wpf"],"backgroundTag":"permission-denied","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"}