c#设置文件权限的方法

  

,,在开发中,我们经常会使用IO操作,例如创建,删除文件等操作。在项目中这样的需求也较多,我们也会经常对这些操作进行编码,但是对文件的权限进行设置,这样的操作可能会手动操作,现在介绍一种采用代码动态对文件设置权限的操作。

  

,,在对文件进行权限设置在DOtNet中,会采用FileSystemAccessRule类进行文件的权限操作。

  

<强> 1。现在看一下FileSystemAccessRule的实现代码:

        公共FileSystemAccessRule (   IdentityReference身份,   FileSystemRights FileSystemRights,   AccessControlType类型)   :这(   的身份,   AccessMaskFromRights (fileSystemRights、类型),   假的,   InheritanceFlags.None,   PropagationFlags.None,   类型)   {   }      公共FileSystemAccessRule (   字符串标识,   FileSystemRights FileSystemRights,   AccessControlType类型)   :这(   新NTAccount(身份)   AccessMaskFromRights (fileSystemRights、类型),   假的,   InheritanceFlags.None,   PropagationFlags.None,   类型)   {   }////构造函数创建文件夹对象的访问规则//公共FileSystemAccessRule (   IdentityReference身份,   FileSystemRights FileSystemRights,   InheritanceFlags InheritanceFlags,   PropagationFlags PropagationFlags,   AccessControlType类型)   :这(   的身份,   AccessMaskFromRights (fileSystemRights、类型),   假的,   inheritanceFlags,   propagationFlags,   类型)   {   }      公共FileSystemAccessRule (   字符串标识,   FileSystemRights FileSystemRights,   InheritanceFlags InheritanceFlags,   PropagationFlags PropagationFlags,   AccessControlType类型)   :这(   新NTAccount(身份)   AccessMaskFromRights (fileSystemRights、类型),   假的,   inheritanceFlags,   propagationFlags,   类型)   {   }   内部FileSystemAccessRule (   IdentityReference身份,   int accessMask,   bool isInherited,   InheritanceFlags InheritanceFlags,   PropagationFlags PropagationFlags,   AccessControlType类型)   :基础(   的身份,   accessMask,   isInherited,   inheritanceFlags,   propagationFlags,   类型)   {   }      # endregion      #地区公共属性      公共FileSystemRights FileSystemRights   {   {返回RightsFromAccessMask(基地。AccessMask);}   }         内部静态int AccessMaskFromRights (FileSystemRights FileSystemRights, AccessControlType controlType)   {   如果(fileSystemRights & lt;(FileSystemRights) 0 | | FileSystemRights祝辞FileSystemRights.FullControl)   把新的ArgumentOutOfRangeException (“fileSystemRights”、环境。fileSystemRights GetResourceString (“Argument_InvalidEnumValue”,“fileSystemRights”));   Contract.EndContractBlock ();      如果(controlType==AccessControlType.Allow) {   fileSystemRights |=FileSystemRights.Synchronize;   }   else if (controlType==AccessControlType.Deny) {   如果(fileSystemRights !=fileSystemRights。FullControl,,   (fileSystemRights fileSystemRights !=ullControl,~ FileSystemRights.DeleteSubdirectoriesAndFiles))   fileSystemRights,=~ FileSystemRights.Synchronize;   }      返回(int) fileSystemRights;   }      内部静态FileSystemRights RightsFromAccessMask (int accessMask)   {   返回(FileSystemRights) accessMask;   }      }      

<强> 2。由于FileSystemAccessRule继承自AccessRule,现在看一下AccessRule的源码:

     ///& lt; summary>///表示用户的标识,访问掩码和访问控制类型(允许或拒绝)的组合。你们;看到cref=" T: System.Security.AccessControl.AccessRule "/比;对象还包含有关子对象如何继承规则以及如何传播继承的信息。///& lt;/summary>   公共抽象类AccessRule: AuthorizationRule   {///& lt; summary>///使用指定的值初始化& lt;看到cref=" T: System.Security.AccessControl.AccessRule "/比;类的一个新实例。///& lt;/summary>///& lt;参数name=吧矸荨弊4怯τ梅梦使嬖虻谋晔丁4瞬问匦胧强梢郧恐谱晃? lt;看到cref=" T: System.Security.Principal.SecurityIdentifier "/比;的对象。;/param> & lt;参数name=" accessMask "在此规则的访问掩码。访问掩码是一个32位的匿名位集合,其含义是由每个集成器定义的。;/param> & lt;参数name=" isInherited祝辞如果此规则继承自父容器,则为真。你们;/param> & lt;参数name=" inheritanceFlags祝辞访问规则的继承属性。;/param> & lt;参数name=" propagationFlags祝辞继承的访问规则是否自动传播。如果& lt; paramref name=" inheritanceFlags "/比;设置为& lt;看到cref="女:System.Security.AccessControl.InheritanceFlags.None "/祝辞,则将忽略传播标志。;/param> & lt;参数name=袄嘈汀钡脑谟行У姆梦士刂评嘈汀?/param> & lt;例外cref=癟: System.ArgumentException”祝辞& lt; paramref name="身份"/比;参数的值不能强制转换为& lt;看到cref=" T: System.Security.Principal.SecurityIdentifier "/祝辞,或者& lt; paramref name="类型"/比;参数包含无效值。;/exception> & lt;例外cref=癟: System.ArgumentOutOfRangeException”祝辞& lt; paramref name=" accessMask "/比;参数的值为零,或者& lt; paramref name=" inheritanceFlags "/比;或& lt; paramref name=" propagationFlags "/比;参数包含无法识别的标志值。;/exception>   保护AccessRule (IdentityReference身份,int accessMask bool isInherited, InheritanceFlags InheritanceFlags, PropagationFlags PropagationFlags, AccessControlType类型);///& lt; summary>///获取与此& lt;参见cref=" T: System.Security.AccessControl.AccessRule "/比;对象关联的& lt;参见cref=" T: System.Security.AccessControl.AccessControlType "/比;对象。///& lt;/summary>//////& lt; returns>///与此& lt;参见cref=" T: System.Security.AccessControl.AccessRule "/比;对象关联的& lt;参见cref=" T: System.Security.AccessControl.AccessControlType "/比;对象。///& lt;/returns>   公共AccessControlType AccessControlType{得到;}   }

c#设置文件权限的方法