Home > other >  Not sure how to use SetAccessControll Function
Not sure how to use SetAccessControll Function

Time:01-30

I was trying to make a simple security application that locked my folders when providing that folders path from file explorer. I did this by making a windows forms application in C#. I've managed to get the GetAccessControl(); Function to work by using this line of code.

System.IO.DirectoryInfo directoryInfo = new DirectoryInfo(path);
System.Security.AccessControl.DirectorySecurity s1 = directoryInfo.GetAccessControl();

I'm not sure how to do this with the SetAccessControl(); function as it requires 2 argumanets to work. I tried implementing this code below:

System.Security.AccessControl.DirectorySecurity s3 = directoryInfo.SetAccessControl(path,s1);

I got an error stating: No overload method for 'SetAccessControl' takes 2 arguments

It is obvious to me why this is not working, but I'm not quite sure how to fix it. If anyone would be willing to help, that would be greatly appreciated.

CodePudding user response:

In .Net 5 :

System.IO.DirectoryInfo directoryInfo = new DirectoryInfo(path);
System.Security.AccessControl.DirectorySecurity s1 = directoryInfo.GetAccessControl();
directoryInfo.SetAccessControl(s1);

or (Changes the security attributes of an existing directory):

FileSystemAclExtensions.SetAccessControl(s1, s2);

In .Net 4.x:

Directory.SetAccessControl(path, s1);

Doc:

https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.setaccesscontrol?view=netframework-4.8.1

https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemaclextensions.setaccesscontrol?view=net-7.0

  • Related