How do I set NTFS permissions in C#.NET? I am trying to change permissions for read/write in .NET. I'm a newbie, please assist!
Reset NTFS security permissions You can use the command: takeown /R /F * before launching the ICACLS. But run the command with caution because it may cause system broken. ICACLS command will reset the permissions of all the folders, files and subfolders.
There are three types of share permissions: Full Control, Change, and Read. Full Control: Enables users to “read,” “change,” as well as edit permissions and take ownership of files. Change: Change means that user can read/execute/write/delete folders/files within share.
you should be able to do it with System.Security.AccessControl name space.
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);
}
Example Call:
//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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With