网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

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

创新互联从2013年成立,是专业互联网技术服务公司,拥有项目网站建设、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元滦平做网站,已为上家服务,为滦平各地企业和个人服务,联系电话:18982081108

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

  1.现在看一下FileSystemAcce***ule的实现代码:

public FileSystemAcce***ule(
            IdentityReference identity,
            FileSystemRights fileSystemRights,
            AccessControlType type )
            : this(
                identity,
                AccessMaskFromRights( fileSystemRights, type ),
                false,
                InheritanceFlags.None,
                PropagationFlags.None,
                type )
        {
        }

        public FileSystemAcce***ule(
            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 FileSystemAcce***ule(
            IdentityReference identity,
            FileSystemRights fileSystemRights,
            InheritanceFlags inheritanceFlags,
            PropagationFlags propagationFlags,
            AccessControlType type )
            : this(
                identity,
                AccessMaskFromRights( fileSystemRights, type ),
                false,
                inheritanceFlags,
                propagationFlags,
                type )
        {
        }

        public FileSystemAcce***ule(
            String identity,
            FileSystemRights fileSystemRights,
            InheritanceFlags inheritanceFlags,
            PropagationFlags propagationFlags,
            AccessControlType type )
            : this(
                new NTAccount(identity),
                AccessMaskFromRights( fileSystemRights, type ),
                false,
                inheritanceFlags,
                propagationFlags,
                type )
        {
        }
        internal FileSystemAcce***ule(
            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;
        }

    }

   2.由于FileSystemAcce***ule继承自Acce***ule,现在看一下Acce***ule的源码:

  /// 
  /// 表示用户的标识、访问掩码和访问控制类型(允许或拒绝)的组合。 对象还包含有关子对象如何继承规则以及如何传播继承的信息。
  /// 
  public abstract class Acce***ule : AuthorizationRule
  {
    /// 
    /// 使用指定的值初始化  类的一个新实例。
    /// 
    /// 应用访问规则的标识。此参数必须是可以强制转换为  的对象。此规则的访问掩码。访问掩码是一个 32 位的匿名位集合,其含义是由每个集成器定义的。如果此规则继承自父容器,则为 true。访问规则的继承属性。继承的访问规则是否自动传播。如果  设置为 ,则将忽略传播标志。有效的访问控制类型。 参数的值不能强制转换为 ,或者  参数包含无效值。 参数的值为零,或者  或  参数包含无法识别的标志值。
    protected Acce***ule(IdentityReference identity, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type);
    /// 
    /// 获取与此  对象关联的  对象。
    /// 
    /// 
    /// 
    /// 与此  对象关联的  对象。
    /// 
    public AccessControlType AccessControlType { get; }
  }

 看了DotNet中实现文件权限设置的操作的类,现在提供几个具体的文件设置操作代码:

 3.获取目录权限列表:

        /// 
         /// 获取目录权限列表
        /// 
        /// 目录的路径。
        /// 指示目录的权限列表
        public IList GetDirectoryPermission(string path)
        {
            try
            {
                if (!DirectoryExists(path))
                    return null;

                IList result = new List();
                var dSecurity = Directory.GetAccessControl(new DirectoryInfo(path).FullName);
                foreach (FileSystemAcce***ule rule in dSecurity.GetAcce***ules(true, true, typeof(NTAccount)))
                    result.Add(rule.FileSystemRights);

                return result;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }

  4.设置目录权限

        /// 
        ///设置目录权限
        /// 
        /// 目录的路径。
        /// 在目录上设置的权限。
        /// 指示是否在目录上应用权限的值。
        public bool SetDirectoryPermission(string path, FileSystemRights permission)
        {
            try
            {
                if (!DirectoryExists(path))
                    return false;

                var acce***ule = new FileSystemAcce***ule("Users", permission,
                                            InheritanceFlags.None,
                                            PropagationFlags.NoPropagateInherit,
                                            AccessControlType.Allow);

                var info = new DirectoryInfo(path);
                var security = info.GetAccessControl(AccessControlSections.Access);

                bool result;
                security.ModifyAcce***ule(AccessControlModification.Set, acce***ule, out result);

                if (!result)
                    return false;

                const InheritanceFlags iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;

                acce***ule = new FileSystemAcce***ule("Users", permission,
                                            iFlags,
                                            PropagationFlags.InheritOnly,
                                            AccessControlType.Allow);

                security.ModifyAcce***ule(AccessControlModification.Add, acce***ule, out result);

                if (!result)
                    return false;

                info.SetAccessControl(security);

                return true;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }

 5.设置目录权限列表

        /// 
        /// 设置目录权限列表
        /// 
        /// 目录的路径。
        /// 在目录上设置的权限。
        /// 指示是否在目录上应用权限的值。
        public bool SetDirectoryPermissions(string path, FileSystemRights[] permissions)
        {
            try
            {
                if (!DirectoryExists(path) || permissions == null || !permissions.Any())
                    return false;

                foreach (var permission in permissions)
                    if (!SetDirectoryPermission(path, permission))
                        return false;

                return true;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }

   以上是对文件权限设置操作的一个简单介绍。

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


网页名称:C#设置文件权限-创新互联
本文路径:http://bjjierui.cn/article/cdecdh.html

其他资讯