peass-ng/PEASS-ng · error · FileReadOnlyException

ERROR_FILE_READ_ONLY

ERROR_FILE_READ_ONLY

Error message

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

What it means

DeleteFileCore throws FileReadOnlyException (derived to include Win32 error 6000, ERROR_FILE_READ_ONLY) when the target file has the ReadOnly/hidden attribute and ignoreReadOnly was not set. Windows refuses to delete read-only files, so AlphaFS surfaces a specific typed exception instead of a generic UnauthorizedAccessException.

Source

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

                  // 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);
                  }

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

                  break;
            }

            // MSDN: .NET 3.5+: IOException:
            // The specified file is in use.
            // There is an open handle on the file, and the operating system is Windows XP or earlier.

            NativeError.ThrowException(lastError, IsDirectory(attributes), pathLp);
         }
      }
   }

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Call File.Delete(path, true) / pass ignoreReadOnly:true to clear the read-only attribute first
  2. Clear the attribute manually before delete: File.SetAttributes(path, attrs & ~FileAttributes.ReadOnly)
  3. Catch FileReadOnlyException and handle the file separately

Example fix

// before
File.Delete(path); // throws on read-only file
// after
var attrs = File.GetAttributes(path);
if ((attrs & FileAttributes.ReadOnly) != 0)
    File.SetAttributes(path, attrs & ~FileAttributes.ReadOnly);
File.Delete(path);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

bool IsReadOnlyFile(string p) => File.GetAttributes(p).HasFlag(FileAttributes.ReadOnly);

Try / catch

try { File.Delete(path); }
catch (FileReadOnlyException) { var a = File.GetAttributes(path); File.SetAttributes(path, a & ~FileAttributes.ReadOnly); File.Delete(path); }

Prevention

When it happens

Trigger: File.Delete or File.DeleteTransacted on a file with FileAttributes.ReadOnly while passing ignoreReadOnly=false (the default).

Common situations: Deleting files copied from CD/DVD or version-control working copies that are marked read-only; temp files created read-only by another tool; cleanup scripts failing on locked read-only artifacts.

Related errors


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