peass-ng/PEASS-ng · error · DirectoryNotFoundException

The system cannot find the path specified: [path]

Error message

The system cannot find the path specified: [path]

What it means

DirectoryNotFoundException ('The system cannot find the path specified') thrown by ExistsDriveOrFolderOrFile when lastError is ERROR_PATH_NOT_FOUND. This means an intermediate directory component of the path does not exist — not merely the final element. AlphaFS translates the Win32 error into the standard .NET exception using the cleaned path.

Source

Thrown at winPEAS/winPEASexe/winPEAS/3rdParty/AlphaFS/Filesystem/Directory Class/Directory.ExistsDrive.cs:86

         var drive = GetDirectoryRootCore(transaction, path, Path.IsPathRooted(path) ? PathFormat.FullPath : PathFormat.RelativePath);

         var driveExists = null != drive && File.ExistsCore(transaction, true, drive, PathFormat.FullPath);

         var regularPath = Path.GetCleanExceptionPath(path);


         if (!driveExists && throwIfDriveNotExists || lastError == Win32Errors.ERROR_NOT_READY)
            throw new DeviceNotReadyException(drive, true);


         throwIfFolderOrFileNotExists = throwIfFolderOrFileNotExists && lastError != Win32Errors.NO_ERROR;

         if (throwIfFolderOrFileNotExists)
         {
            if (lastError != Win32Errors.NO_ERROR)
            {
               if (lastError == Win32Errors.ERROR_PATH_NOT_FOUND)
                  throw new DirectoryNotFoundException(regularPath);


               if (lastError == Win32Errors.ERROR_FILE_NOT_FOUND)
               {
                  if (isFolder)
                     throw new DirectoryNotFoundException(regularPath);

                  throw new FileNotFoundException(regularPath);
               }
            }
         }


         return driveExists;
      }
   }
}

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Create the missing directory first (Directory.CreateDirectory creates the full chain) before asserting it exists.
  2. Correct the configured/hard-coded path to an existing location.
  3. Verify the drive letter itself exists (DriveInfo.GetDrives()) before probing deep paths on it.
  4. Catch DirectoryNotFoundException and treat the path as absent for scan-style code.

Example fix

// before
if (!Directory.ExistsDrive(targetDir)) throw new Exception("missing");
// after
Directory.CreateDirectory(targetDir); // creates intermediate folders
bool exists = Directory.ExistsDrive(targetDir);
Defensive patterns

Strategy: validation

Validate before calling

string root = Path.GetPathRoot(path);
if (root == null || !Directory.Exists(root)) { Log.Warn("drive/root missing: " + path); return; }

Type guard

static bool PathRootExists(string path) { var r = Path.GetPathRoot(path); return r != null && Directory.Exists(r); }

Try / catch

try { ok = Directory.ExistsDrive(path, true, true); } catch (DirectoryNotFoundException ex) { Log.Warn("intermediate path missing", ex); ok = false; }

Prevention

When it happens

Trigger: Directory.ExistsDrive(path) with throwIfFolderOrFileNotExists=true and a path such as "C:\\a\\b\\c" where "C:\\a\\b" (a parent component) is missing; also paths pointing at nonexistent drives' subdirectories.

Common situations: Hard-coded paths that assume an installed product's directory; config files with stale roots after migration; scanning drive letters that don't exist so even the root component is unresolvable.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


AI-assisted analysis of peass-ng/PEASS-ng@53fb989abc (2026-09-02). Data as JSON: /api/errors/bed5c603ecbc2c8d. Report an issue: GitHub.