{"record":{"id":"709d1b5768ab274d","repo":"peass-ng/PEASS-ng","slug":"0-the-target-file-is-a-directory-not-a-file","errorCode":null,"errorMessage":"({0}) The target file is a directory, not a file: [{1}]","messagePattern":"\\((.+?)\\) The target file is a directory, not a file: \\[(.+?)\\]","errorType":"exception","errorClass":"UnauthorizedAccessException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/3rdParty/AlphaFS/Filesystem/File Class/File Core Methods/File.DeleteFileCore.cs","lineNumber":112,"sourceCode":"                  NativeError.ThrowException(lastError, pathLp);\n                  break;\n\n\n               case Win32Errors.ERROR_ACCESS_DENIED:\n\n                  if (attributes == 0)\n                  {\n                     var attrs = new NativeMethods.WIN32_FILE_ATTRIBUTE_DATA();\n\n                     if (FillAttributeInfoCore(transaction, pathLp, ref attrs, false, true) == Win32Errors.NO_ERROR)\n\n                        attributes = attrs.dwFileAttributes;\n                  }\n\n\n                  // MSDN: .NET 3.5+: UnauthorizedAccessException: Path is a directory.\n                  if (IsDirectory(attributes))\n                     throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, \"({0}) {1}\", lastError.ToString(CultureInfo.InvariantCulture), string.Format(CultureInfo.InvariantCulture, Resources.Target_File_Is_A_Directory, pathLp)));\n\n\n                  if (IsReadOnlyOrHidden(attributes))\n                  {\n                     if (ignoreReadOnly)\n                     {\n                        // Reset attributes to Normal.\n                        SetAttributesCore(transaction, false, pathLp, FileAttributes.Normal, PathFormat.LongFullPath);\n\n                        goto startDeleteFile;\n                     }\n\n\n                     // MSDN: .NET 3.5+: UnauthorizedAccessException: Path specified a read-only file.\n                     throw new FileReadOnlyException(pathLp);\n                  }\n\n                  ","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/3rdParty/AlphaFS/Filesystem/File Class/File Core Methods/File.DeleteFileCore.cs#L94-L130","documentation":"DeleteFileCore throws UnauthorizedAccessException when the target of a file delete is actually a directory (localized resource string Target_File_Is_A_Directory). File.Delete must only target files; when the dwFileAttributes indicate a directory, AlphaFS raises this before calling native DeleteFile. Message includes the Win32 lastError code and the resolved long path.","triggerScenarios":"Calling File.Delete or File.DeleteTransacted on a path that resolves to a directory, e.g. when a scan enumerates mixed entries and passes directory paths to File.Delete.","commonSituations":"Iterating Directory.GetFileSystemEntries and deleting everything with File.Delete; path points at a folder created by a previous run; filename/dirname confusion from user input.","solutions":["Use Directory.Delete(path) for directories and File.Delete(path) only for files","Check (File.GetAttributes(path) & FileAttributes.Directory) != 0 before deleting","Filter enumerated entries by type before passing them to File.Delete"],"exampleFix":"// before\nFile.Delete(entry);\n// after\nvar attrs = File.GetAttributes(entry);\nif ((attrs & FileAttributes.Directory) != 0)\n    Directory.Delete(entry, true);\nelse\n    File.Delete(entry);","handlingStrategy":"type-guard","validationCode":"var attrs = File.GetAttributes(path);\nbool isDir = (attrs & FileAttributes.Directory) != 0;\nif (isDir) { Directory.Delete(path, true); return; }","typeGuard":"bool IsDirectory(string p) => File.GetAttributes(p).HasFlag(FileAttributes.Directory);","tryCatchPattern":"try { File.Delete(path); }\ncatch (UnauthorizedAccessException ex) when (ex.Message.Contains(\"directory\")) { Directory.Delete(path, true); }","preventionTips":["Check FileAttributes.Directory before any delete","Filter enumerated entries by type before deletion","Route deletes through one helper that dispatches file vs directory"],"tags":["unauthorized-access","file-vs-directory","io","wrong-api"],"backgroundTag":"file-is-a-directory","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}