Configuring NTFS Permissions in C # .NET

How to set NTFS permissions in C # .NET? I am trying to change read / write permissions in .NET. I'm a newbie, please help!

+4
source share
1 answer

You can do this using the System.Security.AccessControl namespace.

System.Security.AccessControl; public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType) { // Create a new DirectoryInfo object. DirectoryInfo dInfo = new DirectoryInfo(FileName); // Get a DirectorySecurity object that represents the // current security settings. DirectorySecurity dSecurity = dInfo.GetAccessControl(); // Add the FileSystemAccessRule to the security settings. dSecurity.AddAccessRule(new FileSystemAccessRule(Account, Rights, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, ControlType)); // Set the new access settings. dInfo.SetAccessControl(dSecurity); } 

Call example:

 //Get current user string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name; //Deny writing to the file AddDirectorySecurity(@"C:\Users\Phil\Desktop\hello.ini",user, FileSystemRights.Write, AccessControlType.Deny); 
+8
source

Source: https://habr.com/ru/post/921946/


All Articles