.NET How to change Folder/File permission or Access rights?

2.8k Views Asked by At

I want to prevent user from moving/renaming/deleting folders but navigating inside the folder to see its sub folders and files; Also, the code can create sub folders/files inside the folder after setting these access rights. For files, i want also to prevent user to update/move/rename/delete but the app can do this. I played with the DirectorySecurity but i can't produce the desired results.

var directoryInfo = new DirectoryInfo(path);
            var directorySecurity = directoryInfo.GetAccessControl();

            var windowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();
            if (windowsIdentity != null)
            {
                var userName = windowsIdentity.Name;
                directorySecurity.AddAccessRule(new FileSystemAccessRule(userName,
                    FileSystemRights.FullControl, AccessControlType.Deny));
                directorySecurity.AddAccessRule(new FileSystemAccessRule(userName,
                    FileSystemRights.Read | FileSystemRights.ListDirectory | FileSystemRights.CreateDirectories | FileSystemRights.CreateFiles , AccessControlType.Allow));
                directoryInfo.SetAccessControl(directorySecurity);
            }
0

There are 0 best solutions below