{"record":{"id":"17b3e40a12167314","repo":"0x7c13/Notepads","slug":"notepads-does-not-support-file-greater-than-1mb-at","errorCode":null,"errorMessage":"Notepads does not support file greater than 1MB at this moment.","messagePattern":"Notepads does not support file greater than 1MB at this moment\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/Notepads/Utilities/FileSystemUtility.cs","lineNumber":349,"sourceCode":"            catch\r\n            {\r\n                return null;\r\n            }\r\n        }\r\n\r\n        public static async Task<TextFile> ReadFileAsync(string filePath, bool ignoreFileSizeLimit, Encoding encoding)\r\n        {\r\n            StorageFile file = await GetFileAsync(filePath);\r\n            return file == null ? null : await ReadFileAsync(file, ignoreFileSizeLimit, encoding);\r\n        }\r\n\r\n        public static async Task<TextFile> ReadFileAsync(StorageFile file, bool ignoreFileSizeLimit, Encoding encoding = null)\r\n        {\r\n            var fileProperties = await file.GetBasicPropertiesAsync();\r\n\r\n            if (!ignoreFileSizeLimit && fileProperties.Size > 1000 * 1024)\r\n            {\r\n                throw new Exception(ResourceLoader.GetString(\"ErrorMessage_NotepadsFileSizeLimit\"));\r\n            }\r\n\r\n            Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);\r\n\r\n            string text;\r\n            var bom = new byte[4];\r\n\r\n            using (var inputStream = await file.OpenReadAsync())\r\n            using (var stream = inputStream.AsStreamForRead())\r\n            {\r\n                stream.Read(bom, 0, 4); // Read BOM values\r\n                stream.Position = 0; // Reset stream position\r\n\r\n                var reader = CreateStreamReader(stream, bom, encoding);\r\n\r\n                string PeekAndRead()\r\n                {\r\n                    if (encoding == null)\r","sourceCodeStart":331,"sourceCodeEnd":367,"githubUrl":"https://github.com/0x7c13/Notepads/blob/6ec270c134b115d6bff38bc3966784e8893a419c/src/Notepads/Utilities/FileSystemUtility.cs#L331-L367","documentation":"Notepads' FileSystemUtility.ReadFileAsync enforces a hard ~1MB cap (1000*1024 bytes) on any file it reads, throwing a plain System.Exception whose message is the localized resource 'ErrorMessage_NotepadsFileSizeLimit' (FileSystemUtility.cs:347-349). The guard exists because the editor loads the whole document into the WinRT text control in memory, so large files would degrade the UI. The method exposes an explicit 'ignoreFileSizeLimit' bool so trusted callers (e.g. SessionManager restoring a previously-opened file) can bypass it; the default is false, so ordinary opens are bounded.","triggerScenarios":"Calling FileSystemUtility.ReadFileAsync(StorageFile file, bool ignoreFileSizeLimit, Encoding encoding) with ignoreFileSizeLimit == false when file.GetBasicPropertiesAsync().Size is greater than 1024000 bytes. Reached through NotepadsCore.CreateTextEditorAsync (default ignoreFileSizeLimit=false, NotepadsCore.cs:178-180) and the TextEditor reload path (TextEditor.xaml.cs:497 passes false). Not reached via SessionManager.cs:458/468/488, which all pass ignoreFileSizeLimit: true.","commonSituations":"Opening large log files, CSV/JSON data exports, minified JS bundles, SQL dumps, or big generated configs in Notepads. Reopening a file that has grown past 1MB since it was first opened (a previously-valid session file now trips the guard on reload via the non-session path). Any programmatic caller of CreateTextEditorAsync/ReadFileAsync that relies on the default parameter.","solutions":["If you intentionally support large files, call CreateTextEditorAsync(..., ignoreFileSizeLimit: true) / FileSystemUtility.ReadFileAsync(file, ignoreFileSizeLimit: true, encoding), mirroring what SessionManager already does.","Pre-check the size with await file.GetBasicPropertiesAsync() and branch (warn the user, open read-only, or chunk) before invoking ReadFileAsync.","Wrap the call in try/catch (System.Exception) and surface a friendly, actionable message, since the thrown exception is untyped.","If you own the build and want a different ceiling, change the constant 1000 * 1024 in FileSystemUtility.cs:347 to the desired limit, keeping the perf/memory tradeoff in mind.","For session-like trusted flows, route the open through the same ignoreFileSizeLimit: true code path so behavior is consistent with SessionManager."],"exampleFix":"// before (default guard trips on >1MB files)\nvar textFile = await FileSystemUtility.ReadFileAsync(file, ignoreFileSizeLimit: false, encoding: encoding);\n\n// after (trusted caller bypasses the cap, like SessionManager)\nvar textFile = await FileSystemUtility.ReadFileAsync(file, ignoreFileSizeLimit: true, encoding: encoding);","handlingStrategy":"validation","validationCode":"// Run this before FileSystemUtility.ReadFileAsync to avoid the throw entirely.\npublic static async Task<bool> IsWithinSizeLimitAsync(StorageFile file, long limit = 1000L * 1024L)\n{\n    var props = await file.GetBasicPropertiesAsync();\n    return props.Size <= (ulong)limit;\n}\n\n// Usage\nif (!await IsWithinSizeLimitAsync(file))\n{\n    // notify user or set ignoreFileSizeLimit: true deliberately\n    return;\n}\nvar textFile = await FileSystemUtility.ReadFileAsync(file, ignoreFileSizeLimit: false, encoding);","typeGuard":"// C# has no runtime type-narrowing guard relevant here; the decision is value-based.\n// Narrow on the size value instead:\nstatic bool ShouldBypassLimit(BasicProperties props, long limit = 1000L * 1024L)\n    => props.Size > (ulong)limit;","tryCatchPattern":"// The exception is untyped (plain System.Exception), so match by message key.\ntry\n{\n    var textFile = await FileSystemUtility.ReadFileAsync(file, ignoreFileSizeLimit: false, encoding);\n    // use textFile...\n}\ncatch (Exception ex) when (ex.Message == ResourceLoader.GetString(\"ErrorMessage_NotepadsFileSizeLimit\"))\n{\n    // surface a friendly prompt; optionally retry with ignoreFileSizeLimit: true if user consents\n}\ncatch (Exception ex)\n{\n    // handle unrelated read/IO failures separately\n}","preventionTips":["Always resolve file size via GetBasicPropertiesAsync before opening user-selected files of unknown origin.","Pass ignoreFileSizeLimit explicitly (true or false) at every call site rather than relying on the default parameter, so intent is visible.","Keep large-file opens (logs, dumps) on a dedicated code path that sets ignoreFileSizeLimit: true, separate from normal editing opens.","When restoring previously-opened editors (sessions), reuse the ignoreFileSizeLimit: true path that SessionManager already uses.","If you raise the 1000*1024 constant, re-test memory and UI responsiveness with worst-case files before shipping."],"tags":["filesystem","file-size","validation","uwp","winrt","notepads","resource-limit"],"backgroundTag":null,"analyzedSha":"6ec270c134b115d6bff38bc3966784e8893a419c","analyzedAt":"2026-08-13T19:38:22.427Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}