{"record":{"id":"6b83493b5b988f32","repo":"peass-ng/PEASS-ng","slug":"0-the-target-directory-is-a-file-not-a-direct","errorCode":null,"errorMessage":"({0}) The target directory is a file, not a directory: [{1}]","messagePattern":"\\((.+?)\\) The target directory is a file, not a directory: \\[(.+?)\\]","errorType":"exception","errorClass":"DirectoryNotFoundException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/3rdParty/AlphaFS/Filesystem/Directory Class/Directory Core Methods/Directory.DeleteDirectoryNative.cs","lineNumber":64,"sourceCode":"\n            : NativeMethods.RemoveDirectoryTransacted(pathLp, transaction.SafeHandle);\n\n\n         var lastError = Marshal.GetLastWin32Error();\n\n         if (!success)\n         {\n            switch ((uint) lastError)\n            {\n               case Win32Errors.ERROR_DIR_NOT_EMPTY:\n                  // MSDN: .NET 3.5+: IOException: The directory specified by path is not an empty directory. \n                  throw new DirectoryNotEmptyException(pathLp, true);\n\n\n               case Win32Errors.ERROR_DIRECTORY:\n                  // MSDN: .NET 3.5+: DirectoryNotFoundException: Path refers to a file instead of a directory.\n                  if (File.ExistsCore(transaction, false, pathLp, PathFormat.LongFullPath))\n                     throw new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, \"({0}) {1}\", lastError, string.Format(CultureInfo.InvariantCulture, Resources.Target_Directory_Is_A_File, pathLp)));\n                  break;\n\n\n               case Win32Errors.ERROR_PATH_NOT_FOUND:\n                  if (continueOnNotFound)\n                     return;\n                  break;\n\n\n               case Win32Errors.ERROR_SHARING_VIOLATION:\n                  // MSDN: .NET 3.5+: IOException: The directory is being used by another process or there is an open handle on the directory.\n                  NativeError.ThrowException(lastError, pathLp);\n                  break;\n\n\n               case Win32Errors.ERROR_ACCESS_DENIED:\n\n                  if (attributes == 0)","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/3rdParty/AlphaFS/Filesystem/Directory Class/Directory Core Methods/Directory.DeleteDirectoryNative.cs#L46-L82","documentation":"DeleteDirectoryNative throws DirectoryNotFoundException with the formatted message '({0}) The target directory is a file, not a directory: [{1}]' when RemoveDirectory fails with Win32 error ERROR_DIRECTORY and File.ExistsCore confirms the path refers to a file. AlphaFS disambiguates the generic OS error into this specific message so the caller knows the path was a file, not a directory.","triggerScenarios":"Calling Directory.Delete (via DeleteDirectoryCore) on a path that actually points to a regular file; a junction/reparse target that resolved to a file; path-building logic that appends or normalizes into a file path (e.g. Path.Combine with a file path from config).","commonSituations":"Config or database stores a path whose trailing segment is a file name; user-supplied path where the item type was not checked; a symlink/junction that points at a file rather than a directory; a file replaced the directory after an earlier existence check.","solutions":["Verify the path is a directory before deleting: check fsEntryInfo.IsDirectory or Directory.Exists vs File.Exists.","Use File.Delete instead when the target turns out to be a file.","Catch DirectoryNotFoundException and branch on whether the path is a file to handle both cases.","Fix upstream path construction so the directory path does not include a file segment."],"exampleFix":"// before\nDirectory.Delete(userPath); // may actually be a file\n// after\nif (File.Exists(userPath))\n    File.Delete(userPath);\nelse if (Directory.Exists(userPath))\n    Directory.Delete(userPath, true);","handlingStrategy":"type-guard","validationCode":"var isDir = Directory.Exists(path) && !File.Exists(path);\nif (!isDir) throw new InvalidOperationException($\"{path} is not a directory\");","typeGuard":"bool IsDirectoryPath(string p) => Directory.Exists(p) && !File.Exists(p);","tryCatchPattern":"try { Directory.Delete(path, true); }\ncatch (DirectoryNotFoundException) {\n    if (File.Exists(path)) File.Delete(path);\n}","preventionTips":["Check File.Exists vs Directory.Exists before deleting user-supplied paths","Validate trailing path segments from config (a file name where a dir is expected)","Re-check item type close to the call to avoid TOCTOU races","Type the storage of paths so directory paths and file paths are distinct"],"tags":["windows","filesystem","wrong-item-type","delete"],"backgroundTag":"target-is-file-not-directory","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}