{"record":{"id":"bf05e6badde96226","repo":"BCUninstaller/Bulk-Crap-Uninstaller","slug":"not-a-windows-exe-file","errorCode":null,"errorMessage":"Not a Windows .exe file.","messagePattern":"Not a Windows \\.exe file\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"source/KlocTools/Tools/FilesystemTools.cs","lineNumber":25,"sourceCode":"using System.IO;\nusing System.Management;\nusing System.Runtime.InteropServices;\nusing Klocman.Extensions;\n\nnamespace Klocman.Tools\n{\n    public static class FilesystemTools\n    {\n        /// <summary>\n        /// Check the architecture of the executable. E.g. 64bit.\n        /// Returns Unknown if the architecture is unsupported or not specified.\n        /// </summary>\n        /// <param name=\"filename\">Full path to the executable file.</param>\n        public static MachineType CheckExecutableMachineType(string filename)\n        {\n            if (!filename.EndsWith(\".exe\", StringComparison.InvariantCultureIgnoreCase))\n            {\n                throw new IOException(\"Not a Windows .exe file.\");\n            }\n\n            using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))\n            {\n                stream.Position = 0x3c;\n                var fileData = new byte[1024];\n\n                var bytesRead = stream.Read(fileData, 0, 1024);\n\n                for (var i = 0; i < bytesRead; i++)\n                {\n                    // Look for the PE signature (PE\\0\\0)\n                    if (i + 5 >= bytesRead) break;\n                    if (fileData[i] != 0x50) continue;\n                    if (fileData[i + 1] != 0x45 || fileData[i + 2] != 0 || fileData[i + 3] != 0) continue;\n\n                    // Join two bytes representing the architecture\n                    var machineId = fileData[i + 5] << 8 | fileData[i + 4];","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/BCUninstaller/Bulk-Crap-Uninstaller/blob/608321de98e92297377b1eb69029af55c25504a1/source/KlocTools/Tools/FilesystemTools.cs#L7-L43","documentation":"Thrown by FilesystemTools.CheckExecutableMachineType(string filename) when filename does not end in \".exe\" (case-insensitive, invariant culture). The method reads the PE header at offset 0x3C, which only exists in Windows PE executables; non-.exe files are rejected up front with IOException rather than being misparsed.","triggerScenarios":"Calling CheckExecutableMachineType with a path whose extension is not .exe — e.g. a .dll, .com, .scr, or a path with no extension. Note: .dll files DO contain PE headers but are still rejected because of this guard.","commonSituations":"User-selected file that is actually a DLL or a shortcut, file with no extension, path computed from a string that lost its extension, or assuming the tool works on any PE image.","solutions":["Verify the extension is .exe before calling, or branch to a DLL-aware PE reader if you need DLL support.","Validate user input at the UI layer and restrict the open dialog filter to *.exe.","If you genuinely need machine type for a DLL, copy/extend the PE-parsing logic without the extension gate."],"exampleFix":"// before\nvar mt = FilesystemTools.CheckExecutableMachineType(path); // path may be .dll\n\n// after\nif (!path.EndsWith(\".exe\", StringComparison.OrdinalIgnoreCase))\n    return MachineType.Unknown;\nvar mt = FilesystemTools.CheckExecutableMachineType(path);","handlingStrategy":"validation","validationCode":"if (!filename.EndsWith(\".exe\", StringComparison.OrdinalIgnoreCase))\n    return MachineType.Unknown;\nvar mt = FilesystemTools.CheckExecutableMachineType(filename);","typeGuard":"static bool IsExePath(string path)\n    => path != null && path.EndsWith(\".exe\", StringComparison.OrdinalIgnoreCase);","tryCatchPattern":"try { return FilesystemTools.CheckExecutableMachineType(path); }\ncatch (IOException) { return MachineType.Unknown; }","preventionTips":["Restrict file pickers to *.exe when architecture detection is required.","Confirm the extension before invoking rather than relying on the throw.","If DLL support is needed, implement a dedicated PE reader without the extension gate."],"tags":["filesystem","pe-header","validation","csharp","kloctools","windows"],"backgroundTag":null,"analyzedSha":"608321de98e92297377b1eb69029af55c25504a1","analyzedAt":"2026-08-13T12:17:35.389Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}