Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
like image 825
Butters Avatar asked Mar 09 '26 14:03

Butters


1 Answers

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.

like image 167
SeToY Avatar answered Mar 11 '26 03:03

SeToY