Disable inheritance on a folder NTFS permissions in C#
Asked Answered
P

1

5

How can I disable inheritance on a folder and delete all inherited permissions

Thank you

I tried this :

DirectoryInfo dInfo = new DirectoryInfo(path);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.SetAccessRuleProtection(false, false);
Phosphorism answered 8/12, 2015 at 17:50 Comment(6)
Don't be a help vampire. Show us some research and what you've tried, and explain why it hasn't worked. Help us to help you.Airfield
I tried this : DirectoryInfo dInfo = new DirectoryInfo(path); DirectorySecurity dSecurity = dInfo.GetAccessControl(); dSecurity.SetAccessRuleProtection(false, false);Phosphorism
Don't put that in the comments -- update your question. Also include how it didn't work.Airfield
So what's wrong with the code you have?Airfield
nothing append and I have no error, I don't think I have to use SetAccessRuleProtection but I don't find an other thingPhosphorism
Please provide a good minimal reproducible example that reliably reproduces whatever problem you're having. Please also provide a precise explanation of what that code does and what you want it to do instead.Gassing
P
13

If someone search the solution :

        DirectorySecurity directorySecurity = Directory.GetAccessControl(path);
        directorySecurity.SetAccessRuleProtection(true, keepPermissions);
        Directory.SetAccessControl(path, directorySecurity);

with keepPermissions true if you want to keep inherited permissions.

Phosphorism answered 9/12, 2015 at 10:54 Comment(3)
Your questions says that you want to "delete all inherited permissions". In what way does posting code that allows you to "keep inherited permissions" qualify as an answer to the question you asked?Gassing
Because if someone want to keep permissions and juste delete inheritance, he has the answer too. Is it a problem ?Phosphorism
To save some other time, for those using .NET Standard 2.0, use DirectoryInfo.Get/SetAccessControl methods.Prepare

© 2022 - 2024 — McMap. All rights reserved.