I'm trying to change the permissions of a file in .NET Core.
However, it seems that FileInfo doesn't have any SetAccessControl
anymore.
// Create a new FileInfo object.
FileInfo fInfo = new FileInfo(FileName);
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = fInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
fSecurity.AddAccessRule(new FileSystemAccessRule(Account,
Rights,
ControlType));
// Set the new access settings.
fInfo.SetAccessControl(fSecurity);
The goal is just to add execution right to the current owner of a file (which is not Windows or Unix specific feature).
Any clues on how to do that on .NET Core ?
How to Get and modify User Group Other Rights on Windows
I finally implement the Windows file permission access:
1. Get the file security:
2. Get the authorization rules:
3. Get the authorization rules for the owner:
4. Add a rule for owner:
5. Bonus
How to get the
group
andothers
, or ... my definition of something equivalent ?Note: This code comes from my open source project Lx.Shell
At this time there are two extension methods:
GetAccessControl
andSetAccessControl
, forFileInfo
,DirectoryInfo
and etc.So you can use
var ac = new FileInfo(path).GetAccessControl()
, this expression is valid both in .NET Framework and .Net Core. But you still needdotnet add package System.IO.FileSystem.AccessControl
.File.GetAccessControl
isn't available in .NET Core.ref: https://docs.microsoft.com/dotnet/api/system.io.filesystemaclextensions.getaccesscontrol
The
FileSecurity
class is now part of the System.IO.FileSystem.AccessControl package for .NET Core. There is no longer aFile.GetAccessControl
method so you will need to instanciate theFileSecurity
instance yourself.Another way to handle acls for directory or file: