{"record":{"id":"8e18267ffbab1083","repo":"BCUninstaller/Bulk-Crap-Uninstaller","slug":"failed-to-get-mainmodule-directory","errorCode":null,"errorMessage":"Failed to get MainModule Directory","messagePattern":"Failed to get MainModule Directory","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"warning","filePath":"source/BulkCrapUninstaller/Forms/Helpers/TargetWindow.cs","lineNumber":72,"sourceCode":"\r\n        private void WindowTargeter1OnPickingStarted(object sender, EventArgs e)\r\n        {\r\n            _setMainWindowVisible(false);\r\n        }\r\n\r\n        private void WindowTargeterWindowSelected(object sender, Klocman.Subsystems.WindowHoverEventArgs e)\r\n        {\r\n            _setMainWindowVisible(true);\r\n\r\n            try\r\n            {\r\n                var fileName = e.TargetWindow.GetRunningProcess().MainModule?.FileName;\r\n                if (fileName == null)\r\n                    throw new InvalidOperationException(\"Process has no MainModule\");\r\n\r\n                var parentDirectory = new FileInfo(fileName).Directory;\r\n                if (parentDirectory == null)\r\n                    throw new InvalidOperationException(\"Failed to get MainModule Directory\");\r\n\r\n                // Ignore targeting BCU itself\r\n                if (PathTools.SubPathIsInsideBasePath(Program.AssemblyLocation.FullName, fileName, true, true))\r\n                    return;\r\n\r\n                OnDirectoriesSelected(new DirectoriesSelectedEventArgs(parentDirectory.ToEnumerable().ToList()));\r\n            }\r\n            catch (Exception exception)\r\n            {\r\n                Console.WriteLine(exception);\r\n                PremadeDialogs.GenericError(exception);\r\n            }\r\n        }\r\n\r\n        public static ICollection<DirectoryInfo> ShowDialog(IWin32Window owner, Action<bool> setMainWindowVisible)\r\n        {\r\n            using (var window = new TargetWindow())\r\n            {\r","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/BCUninstaller/Bulk-Crap-Uninstaller/blob/608321de98e92297377b1eb69029af55c25504a1/source/BulkCrapUninstaller/Forms/Helpers/TargetWindow.cs#L54-L90","documentation":"The targeted process's MainModule filename was obtained, but new FileInfo(fileName).Directory returned null. FileInfo.Directory is only null when the path is a filesystem root (e.g. \"C:\\\"), so this indicates the module path resolved to a drive root. It is caught and shown as a generic error.","triggerScenarios":"Targeting a window whose process image path is a bare drive root; edge cases where GetRunningProcess returns a synthetic or malformed path; a driver/root-resident process.","commonSituations":"Extremely rare in practice; only occurs for processes whose image path is a root directory.","solutions":["Target a different window whose process image lives in a real subdirectory.","Fall back to browsing the directory manually.","If extending the code, check FileInfo.Directory for null before use and degrade gracefully."],"exampleFix":"// before\nvar parentDirectory = new FileInfo(fileName).Directory;\nif (parentDirectory == null)\n    throw new InvalidOperationException(\"Failed to get MainModule Directory\");\n// after\nvar dir = new FileInfo(fileName).Directory;\nif (dir == null) { PremadeDialogs.GenericError(new Exception(\"Root path\")); return; }","handlingStrategy":"validation","validationCode":"var dir = new FileInfo(fileName).Directory;\nif (dir == null) { /* root path: cannot target a directory */ return; }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check FileInfo.Directory for null before relying on it.","Treat root-resident process images as non-targetable."],"tags":["window-targeting","filesystem","edge-case"],"backgroundTag":null,"analyzedSha":"608321de98e92297377b1eb69029af55c25504a1","analyzedAt":"2026-08-13T12:17:35.389Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}