tags:

views:

33

answers:

2

How can I automate the following manual steps in C#?

  • Right click a folder in Windows Explorer;

    Properties -> Security -> Advanced -> Edit

  • Un-tick "Include inheritable permissions from this object's parent" and click Remove.

  • Click Add, choose a group and grant it Modify rights.

I've found this article, which looks like exactly what i need, but I don't have and cant find Microsoft.Win32.Security

Thanks

+1  A: 

I don't know about that one, but you should be able to do that via the DirectorySecurity class in the System.Security.AccessControl namespace.

And I assume you'd probably want to look at the InheritanceFlags enumeration as well.

ho1
thanks for the answer, id have accepted both if i could
Andrew Bullock
@Andrew: No problem, Aneef obviously put more effort into his to make it a more comprehensive answer so makes sense that his is accepted.
ho1
+1  A: 

check the code below:

DirectoryInfo dInfo = new DirectoryInfo(strFullPath);

DirectorySecurity dSecurity = dInfo.GetAccessControl();

dSecurity.SetAccessRuleProtection(true, true); //check off & copy inherited security setting

dInfo.SetAccessControl(dSecurity);

http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.objectsecurity.setaccessruleprotection.aspx

and this for setting permissions on a folder :

http://www.redmondpie.com/applying-permissions-on-any-windows-folder-using-c/

Aneef