• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > 采用C#代码动态设置文件权限

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

作者:彭泽0902 字体:[增加 减小] 来源:互联网 时间:2017-05-28

彭泽0902 通过本文主要向大家介绍了采用type-c的手机,采用c/s架构,采用c/s模式工作的是,采用tn c接地系统时,没有采用type c接口等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

   2.由于FileSystemAccessRule继承自AccessRule,现在看一下AccessRule的源码:

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

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

   3.获取目录权限列表:

 /// <summary>
  /// 获取目录权限列表
  /// </summary>
  /// <param name="path">目录的路径。</param>
  /// <returns>指示目录的权限列表</returns>
  public IList<FileSystemRights> GetDirectoryPermission(string path)
  {
   try
   {
    if (!DirectoryExists(path))
     return null;
    IList<FileSystemRights> result = new List<FileSystemRights>();
    var dSecurity = Directory.GetAccessControl(new DirectoryInfo(path).FullName);
    foreach (FileSystemAccessRule rule in dSecurity.GetAccessRules(true, true, typeof(NTAccount)))
     result.Add(rule.FileSystemRights);
    return result;
   }
   catch (Exception e)
   {
    throw new Exception(e.Message, e);
   }
  }
</div>

 4.设置目录权限

 /// <summary>
  ///设置目录权限
  /// </summary>
  /// <param name="path">目录的路径。</param>
  /// <param name="permission">在目录上设置的权限。</param>
  /// <returns>指示是否在目录上应用权限的值。</returns>
  public bool SetDirectoryPermission(string path, FileSystemRights permission)
  {
   try
   {
    if (!DirectoryExists(path))
     return false;
    var accessRule = new FileSystemAccessRule("Users", permission,
           InheritanceFlags.None,
           PropagationFlags.NoPropagateInherit,
           AccessControlType.Allow);

    var info = new DirectoryInfo(path);
    var security = info.GetAccessControl(AccessControlSections.Access);
    bool result;
    security.ModifyAccessRule(AccessControlModification.Set, accessRule, out result);
    if (!result)
     return false;
    const InheritanceFlags iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
    accessRule = new FileSystemAccessRule("Users", permission,
   



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

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

相关文章

  • 2017-05-28使用HttpHanlder处理404:File not found的问题
  • 2017-05-28C# String Replace高效的实例方法
  • 2017-05-28C#中矩阵运算方法实例分析
  • 2017-05-28C#中String与string的区别分析
  • 2017-05-28c#判断输入的是不是数字的小例子
  • 2017-05-28c#标准idispose模式使用示例
  • 2017-05-28c#高效比对大量图片的实例代码
  • 2017-05-28C#中datatable序列化与反序列化实例分析
  • 2017-05-28c#使用filesystemwatcher实时监控文件目录的添加和删除
  • 2017-05-28C# BackgroundWorker用法详解

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 浅谈Silverlight 跨线程的使用详解
    • C#模拟MSN窗体抖动的实现代码
    • C#多线程学习之(五)使用定时器进行多线程的自动管理
    • C#使用二分查找法判断指定字符的方法
    • C#动态执行字符串(动态创建代码)的实例代码
    • C#异步编程详解
    • C#中创建PDF网格并插入图片的方法
    • C# 对象持久化详解
    • C#类的创建与初始化实例解析
    • C#搜索文字在文件及文件夹中出现位置的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有