Monday 23 April 2012

Beginning .Net , C# Tips : Removing Rules to Access Control List on File and Directory using C# Programming

To remove rule you can use RemoveAccessRule methods.
Here are sample Example.

C# Example :
        string strFilePath = "D:\\Others\\bookmarks.html";

        System.Security.AccessControl.FileSecurity sec = System.IO.File.GetAccessControl(strFilePath);

        sec.RemoveAccessRule(new System.Security.AccessControl.FileSystemAccessRule(@"Everyone", System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));

        System.IO.File.SetAccessControl(strFilePath, sec);

If you open the file Properties dialog again, you see that the user has been removed from the Access
Control List.

Here are converted code for vb.net with automated tool.
        string strFilePath = "D:\Others\bookmarks.html";

        System.Security.AccessControl.FileSecurity sec = System.IO.File.GetAccessControl(strFilePath);

        sec.RemoveAccessRule(new System.Security.AccessControl.FileSystemAccessRule(@"Everyone", System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));

        System.IO.File.SetAccessControl(strFilePath, sec);

No comments:

Post a Comment