peass-ng/PEASS-ng · error · DirectoryReadOnlyException

ERROR_FILE_READ_ONLY

ERROR_FILE_READ_ONLY

Error message

({145}) The file is read-only: [{pathLp}]

What it means

DeleteDirectoryNative throws DirectoryReadOnlyException with message '({145}) The file is read-only: [{pathLp}]' when the directory (or its items) carries the read-only attribute and deletion of the read-only item was not permitted (ignoreReadOnly false). Win32 reports ERROR_FILE_READ_ONLY / access issues for read-only items; AlphaFS maps it to DirectoryReadOnlyException after an attempt to clear attributes failed.

Source

Thrown at winPEAS/winPEASexe/winPEAS/3rdParty/AlphaFS/Filesystem/Directory Class/Directory Core Methods/Directory.DeleteDirectoryNative.cs:106

                        attributes = attrs.dwFileAttributes;
                  }


                  if (File.IsReadOnlyOrHidden(attributes))
                  {
                     // MSDN: .NET 3.5+: IOException: The directory specified by path is read-only.

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

                        goto startRemoveDirectory;
                     }


                     // MSDN: .NET 3.5+: IOException: The directory is read-only.
                     throw new DirectoryReadOnlyException(pathLp);
                  }


                  // MSDN: .NET 3.5+: UnauthorizedAccessException: The caller does not have the required permission.
                  if (attributes == 0)
                     NativeError.ThrowException(lastError, File.IsDirectory(attributes), pathLp);

                  break;
            }

            // MSDN: .NET 3.5+: IOException:
            // A file with the same name and location specified by path exists.
            // The directory specified by path is read-only, or recursive is false and path is not an empty directory. 
            // The directory is the application's current working directory. 
            // The directory contains a read-only file.
            // The directory is being used by another process.

            NativeError.ThrowException(lastError, pathLp);

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Retry the delete with the ignoreReadOnly option enabled so AlphaFS clears the read-only attribute first.
  2. Clear attributes yourself: File.SetAttributes(path, FileAttributes.Normal) on offending items before deletion.
  3. Remove the read-only attribute recursively (attrib -r /s /d or per-item) before Directory.Delete.
  4. Take ownership/ACLs into account if attribute clearing fails due to permissions.

Example fix

// before
Directory.Delete(roDir, true); // throws on read-only items
// after
foreach (var f in Directory.GetFiles(roDir, "*", SearchOption.AllDirectories))
    File.SetAttributes(f, FileAttributes.Normal);
File.SetAttributes(roDir, FileAttributes.Directory);
Directory.Delete(roDir, true);
Defensive patterns

Strategy: try-catch

Validate before calling

var attrs = File.GetAttributes(path);
if ((attrs & FileAttributes.ReadOnly) != 0)
    File.SetAttributes(path, attrs & ~FileAttributes.ReadOnly);

Try / catch

try { Directory.Delete(path, true); }
catch (DirectoryReadOnlyException) {
    File.SetAttributes(path, FileAttributes.Normal);
    Directory.Delete(path, true);
}

Prevention

When it happens

Trigger: Calling Directory.Delete / DeleteDirectoryCore on a directory marked read-only (FILE_ATTRIBUTE_READONLY) without ignoreReadOnly:true; deleting a folder containing read-only files where the recursive path attempted attribute clearing but the OS still refused; junction/special folders where Windows sets read-only on the directory itself.

Common situations: Folders copied from read-only media (CD/DVD) retain the read-only attribute; source-control or tool-set read-only flags; backup restores preserving read-only attributes; Windows system folders (some use read-only as a marker for customized/desktop.ini behavior).

Related errors


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