资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#设置文件权限的方法-创新互联

   在开发中,我们经常会使用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;
    }

  }

当前标题:C#设置文件权限的方法-创新互联
文章链接:http://cdkjz.cn/article/pjios.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220