Setting NTFS Permissions with C#

原创 2004年09月29日 11:41:00
Today I needed to set NTFS permissions in C# on some newly created directories.

No problem I thought, the CLR will have something for it somewhere in Security, so I checked Google in the hopes to find which class to use.

But Google didn't find anything... This amazed me. "Why can't I control NTFS permissions with .NET ?!?"

After looking for an hour or so, I found a GotDotNet User Sample, called 'ACLs in .NET'. Finally I thought, now it's going to be plug in and set rights.

Well this library is great. It makes settings NTFS rights so easy.

But it lacks a bit in documentation. Therefore I'm providing some of the code I used with it, it could help you. (or it could show my possibly bad coding style, as far as my knowledge goes for know, it should be fine)

Reference the dll, and use it.

using Microsoft.Win32.Security;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Here's a method to add a dir, and set NTFS permissions on it for a given user:

private Boolean CreateDir(String strSitePath, String strUserName) {

       Boolean bOk;

       try {


              SecurityDescriptor secDesc = SecurityDescriptor.GetFileSecurity(strSitePath, SECURITY_INFORMATION.DACL_SECURITY_INFORMATION);

              Dacl dacl = secDesc.Dacl;

              Sid sidUser = new Sid (strUserName);


              // allow: folder, subfolder and files

              // modify

              dacl.AddAce (new AceAccessAllowed (sidUser, AccessType.GENERIC_WRITE | AccessType.GENERIC_READ | AccessType.DELETE | AccessType.GENERIC_EXECUTE , AceFlags.OBJECT_INHERIT_ACE | AceFlags.CONTAINER_INHERIT_ACE));


              // deny: this folder

              // write attribs

              // write extended attribs

              // delete

              // change permissions

              // take ownership

              DirectoryAccessType DAType = DirectoryAccessType.FILE_WRITE_ATTRIBUTES | DirectoryAccessType.FILE_WRITE_EA | DirectoryAccessType.DELETE | DirectoryAccessType.WRITE_OWNER | DirectoryAccessType.WRITE_DAC;

              AccessType AType = (AccessType)DAType;

              dacl.AddAce (new AceAccessDenied (sidUser, AType));



              secDesc.SetFileSecurity(strSitePath, SECURITY_INFORMATION.DACL_SECURITY_INFORMATION);

              bOk = true;

       } catch {

              bOk = false;


       return bOk;

} /* CreateDir */

The AceFlags

determine the level of inheritance on the object.

And the DirectoryAccessType is used to create a AccessType with some permissions not in the AccessType enum.

I hope this is useful.

posted on Sunday, February 08, 2004 12:06 AM

Setting NTFS Permissions with C#[

Setting NTFS Permissions with C#[转] Today I needed to set NTFS permissions in C# on some newly creat...
  • wenshengwang
  • wenshengwang
  • 2007年11月01日 10:10
  • 448

Setting NTFS Permissions with C#

 Setting NTFS Permissions with C#Today I needed to set NTFS permissions in C# on some newly created ...
  • dreamlins
  • dreamlins
  • 2007年02月08日 14:05
  • 390

WinSCP:but error occurred while setting the permissions and/or timestamp

错误的信息为: Upload of file .. was successful, but error occurred while setting the permissions and/or t...
  • w5688414
  • w5688414
  • 2017年11月06日 13:16
  • 211

AIX 安装 RAC 错误集锦(7)--- CRS清理

AIX RAC  CRS安装错误案例环境:操作系统: AIX 5.3.8集群软件: CRS数据库:   Oracle、案例在一台安装过crs的aix 节点上重新安...
  • lqx0405
  • lqx0405
  • 2015年03月31日 11:57
  • 329

AD Group Type and Permissions Setting in Cognos

在与AD账号集成以后,Cognos不仅可以使用用户的个人AD账号来设置权限,也可以使用AD的用户组。 使用AD用户组的好处是在对报表按照文件夹分类的时候,可以把文件夹...
  • u012988208
  • u012988208
  • 2016年03月07日 15:19
  • 378

Tomcat无法安装 Check your settings and permissions Ignore and continue anyway

这几天装tomcat时竟然遇到了这个问题,网上查了下,解决方法如下:在cmd下敲两条命令:sc delete tomcat5 和sc delete tomcat6,显示success后就表示你已经成功...
  • cherry_sun
  • cherry_sun
  • 2010年01月11日 16:49
  • 8985

oracle 10g rac安装报错汇总

删除crs Oracle CRS stack is already configured and will berunning under init(1M) Failure at final chec...
  • u011538954
  • u011538954
  • 2014年03月06日 23:16
  • 1182

Setting file and folder permissions

There are a few examples of this already on the web but most of them are over complicated and dont j...
  • seucbh
  • seucbh
  • 2013年09月09日 13:41
  • 884

[开源]文件夹比较和同步工具 FreeFileSync

FreeFileSync开放源代码的一个文件夹比较和同步工具 官方站点 : Key Features Det...
  • islandstar
  • islandstar
  • 2012年07月28日 10:28
  • 894

Rabbitmq for openstack

OpenStack使用消息队列在不同的服务之间协调操作和状态信息 OpenStack支持多种消息服务软件:RabbitMQ, Qpid,and ZeroMQ. 而 RabbitMQ是流行的开源消息队列...
  • S1234567_89
  • S1234567_89
  • 2016年06月22日 14:18
  • 1050
您举报文章:Setting NTFS Permissions with C#