C#: Remove Read-only attribute from File without Administrative Privilege

10,066

You need to have read/write permission on the file.

I preferably use a method like this:

FileSystemInfo fsi = new FileSystemInfo(pathToFile);
fsi.Attributes = FileAttributes.Normal;

or

File.SetAttributes(pathToFile, FileAttributes.Normal);

But as I've said, this won't be possible without read/write permissions on the specific file.

Share:
10,066
Butters
Author by

Butters

Updated on June 05, 2022

Comments

  • Butters
    Butters almost 2 years

    Is there a way to remove read-only attribute of a file if the user is not an administrator?

    This works if you're an admin but what if you're not?

    FileInfo myFile = new FileInfo(pathToFile);
    myFile.IsReadOnly = false;
    
    • Steve
      Steve about 11 years
      That works if you have read/write permission on file. The administrator role is not the key.
    • Damien_The_Unbeliever
      Damien_The_Unbeliever about 11 years
      This is a permissions issue, not an Admin/non Admin issue, per se. If the user account you're running under has the "Write Attributes" permission on the file, they'll be able to make this change.
    • Butters
      Butters about 11 years
      Thanks. I understand it now.