peass-ng/PEASS-ng · error · UnauthorizedAccessException

( ) The target file is a directory, not a file: [ ]

Error message

({0}) The target file is a directory, not a file: [{1}]

What it means

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.

Solutions

  1. Use Directory.Delete(path) for directories and File.Delete(path) only for files
  2. Check (File.GetAttributes(path) & FileAttributes.Directory) != 0 before deleting
  3. Filter enumerated entries by type before passing them to File.Delete

Example fix

// before
File.Delete(entry);
// after
var attrs = File.GetAttributes(entry);
if ((attrs & FileAttributes.Directory) != 0)
    Directory.Delete(entry, true);
else
    File.Delete(entry);
Defensive patterns

Strategy: type-guard

Validate before calling

var attrs = File.GetAttributes(path);
bool isDir = (attrs & FileAttributes.Directory) != 0;
if (isDir) { Directory.Delete(path, true); return; }

Type guard

bool IsDirectory(string p) => File.GetAttributes(p).HasFlag(FileAttributes.Directory);

Try / catch

try { File.Delete(path); }
catch (UnauthorizedAccessException ex) when (ex.Message.Contains("directory")) { Directory.Delete(path, true); }

Prevention

When it happens

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

Common situations: 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.

Related errors


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

Appendix: source

Thrown at winPEAS/winPEASexe/winPEAS/3rdParty/AlphaFS/Filesystem/File Class/File Core Methods/File.DeleteFileCore.cs:112

                  NativeError.ThrowException(lastError, pathLp);
                  break;


               case Win32Errors.ERROR_ACCESS_DENIED:

                  if (attributes == 0)
                  {
                     var attrs = new NativeMethods.WIN32_FILE_ATTRIBUTE_DATA();

                     if (FillAttributeInfoCore(transaction, pathLp, ref attrs, false, true) == Win32Errors.NO_ERROR)

                        attributes = attrs.dwFileAttributes;
                  }


                  // MSDN: .NET 3.5+: UnauthorizedAccessException: Path is a directory.
                  if (IsDirectory(attributes))
                     throw new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, "({0}) {1}", lastError.ToString(CultureInfo.InvariantCulture), string.Format(CultureInfo.InvariantCulture, Resources.Target_File_Is_A_Directory, pathLp)));


                  if (IsReadOnlyOrHidden(attributes))
                  {
                     if (ignoreReadOnly)
                     {
                        // Reset attributes to Normal.
                        SetAttributesCore(transaction, false, pathLp, FileAttributes.Normal, PathFormat.LongFullPath);

                        goto startDeleteFile;
                     }


                     // MSDN: .NET 3.5+: UnauthorizedAccessException: Path specified a read-only file.
                     throw new FileReadOnlyException(pathLp);
                  }

                  

View on GitHub (pinned to 53fb989abc)