BCUninstaller/Bulk-Crap-Uninstaller · warning · InvalidOperationException

Failed to get MainModule Directory

Error message

Failed to get MainModule Directory

What it means

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.

Source

Thrown at source/BulkCrapUninstaller/Forms/Helpers/TargetWindow.cs:72

        private void WindowTargeter1OnPickingStarted(object sender, EventArgs e)
        {
            _setMainWindowVisible(false);
        }

        private void WindowTargeterWindowSelected(object sender, Klocman.Subsystems.WindowHoverEventArgs e)
        {
            _setMainWindowVisible(true);

            try
            {
                var fileName = e.TargetWindow.GetRunningProcess().MainModule?.FileName;
                if (fileName == null)
                    throw new InvalidOperationException("Process has no MainModule");

                var parentDirectory = new FileInfo(fileName).Directory;
                if (parentDirectory == null)
                    throw new InvalidOperationException("Failed to get MainModule Directory");

                // Ignore targeting BCU itself
                if (PathTools.SubPathIsInsideBasePath(Program.AssemblyLocation.FullName, fileName, true, true))
                    return;

                OnDirectoriesSelected(new DirectoriesSelectedEventArgs(parentDirectory.ToEnumerable().ToList()));
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                PremadeDialogs.GenericError(exception);
            }
        }

        public static ICollection<DirectoryInfo> ShowDialog(IWin32Window owner, Action<bool> setMainWindowVisible)
        {
            using (var window = new TargetWindow())
            {

View on GitHub (pinned to 608321de98)

Solutions

  1. Target a different window whose process image lives in a real subdirectory.
  2. Fall back to browsing the directory manually.
  3. If extending the code, check FileInfo.Directory for null before use and degrade gracefully.

Example fix

// before
var parentDirectory = new FileInfo(fileName).Directory;
if (parentDirectory == null)
    throw new InvalidOperationException("Failed to get MainModule Directory");
// after
var dir = new FileInfo(fileName).Directory;
if (dir == null) { PremadeDialogs.GenericError(new Exception("Root path")); return; }
Defensive patterns

Strategy: validation

Validate before calling

var dir = new FileInfo(fileName).Directory;
if (dir == null) { /* root path: cannot target a directory */ return; }

Prevention

When it happens

Trigger: 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.

Common situations: Extremely rare in practice; only occurs for processes whose image path is a root directory.

Related errors


AI-assisted analysis of BCUninstaller/Bulk-Crap-Uninstaller@608321de98 (2026-08-13). Data as JSON: /api/errors/8e18267ffbab1083. Report an issue: GitHub.