采用C#代码动态设置文件权限

2019-12-30 15:02:54刘景俊

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

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

    1.现在看一下FileSystemAccessRule的实现代码:


 public FileSystemAccessRule(
   IdentityReference identity,
   FileSystemRights fileSystemRights,
   AccessControlType type )
   : this(
    identity,
    AccessMaskFromRights( fileSystemRights, type ),
    false,
    InheritanceFlags.None,
    PropagationFlags.None,
    type )
  {
  }
  public FileSystemAccessRule(
   String identity,
   FileSystemRights fileSystemRights,
   AccessControlType type )
   : this(
    new NTAccount(identity),
    AccessMaskFromRights( fileSystemRights, type ),
    false,
    InheritanceFlags.None,
    PropagationFlags.None,
    type )
  {
  }
  //
  // Constructor for creating access rules for folder objects
  //
  public FileSystemAccessRule(
   IdentityReference identity,
   FileSystemRights fileSystemRights,
   InheritanceFlags inheritanceFlags,
   PropagationFlags propagationFlags,
   AccessControlType type )
   : this(
    identity,
    AccessMaskFromRights( fileSystemRights, type ),
    false,
    inheritanceFlags,
    propagationFlags,
    type )
  {
  }
  public FileSystemAccessRule(
   String identity,
   FileSystemRights fileSystemRights,
   InheritanceFlags inheritanceFlags,
   PropagationFlags propagationFlags,
   AccessControlType type )
   : this(
    new NTAccount(identity),
    AccessMaskFromRights( fileSystemRights, type ),
    false,
    inheritanceFlags,
    propagationFlags,
    type )
  {
  }
  internal FileSystemAccessRule(
   IdentityReference identity,
   int accessMask,
   bool isInherited,
   InheritanceFlags inheritanceFlags,
   PropagationFlags propagationFlags,
   AccessControlType type )
   : base(
    identity,
    accessMask,
    isInherited,
    inheritanceFlags,
    propagationFlags,
    type )
  {
  }
  #endregion
  #region Public properties
  public FileSystemRights FileSystemRights
  {
   get { return RightsFromAccessMask( base.AccessMask ); }
  }
  internal static int AccessMaskFromRights( FileSystemRights fileSystemRights, AccessControlType controlType )
  {
   if (fileSystemRights < (FileSystemRights) 0 || fileSystemRights > FileSystemRights.FullControl)
    throw new ArgumentOutOfRangeException("fileSystemRights", Environment.GetResourceString("Argument_InvalidEnumValue", fileSystemRights, "FileSystemRights"));
   Contract.EndContractBlock();

   if (controlType == AccessControlType.Allow) {
    fileSystemRights |= FileSystemRights.Synchronize;
   }
   else if (controlType == AccessControlType.Deny) {
    if (fileSystemRights != FileSystemRights.FullControl &&
     fileSystemRights != (FileSystemRights.FullControl & ~FileSystemRights.DeleteSubdirectoriesAndFiles))
     fileSystemRights &= ~FileSystemRights.Synchronize;
   }
   return ( int )fileSystemRights;
  }
  internal static FileSystemRights RightsFromAccessMask( int accessMask )
  {
   return ( FileSystemRights )accessMask;
  }
 }