zoukankan      html  css  js  c++  java
  • 一个 C# 文件权限的帮助类

    直接贴代码了:

    FilePermissionHelper.cs

    using System.Collections.Generic;
    using System.IO;
    using System.Security.AccessControl;
    using System.Security.Principal;
    
    namespace MvcSample.Extensions
    {
        /// <summary>
        /// File permission helper
        /// </summary>
        public static class FilePermissionHelper
        {
            /// <summary>
            /// Check permissions
            /// </summary>
            /// <param name="path">Path</param>
            /// <param name="checkRead">Check read</param>
            /// <param name="checkWrite">Check write</param>
            /// <param name="checkModify">Check modify</param>
            /// <param name="checkDelete">Check delete</param>
            /// <returns>Result</returns>
            public static bool CheckPermissions(string path, bool checkRead, bool checkWrite, bool checkModify, bool checkDelete)
            {
                bool flag = false;
                bool flag2 = false;
                bool flag3 = false;
                bool flag4 = false;
                bool flag5 = false;
                bool flag6 = false;
                bool flag7 = false;
                bool flag8 = false;
                WindowsIdentity current = WindowsIdentity.GetCurrent();
                AuthorizationRuleCollection rules;
                try
                {
                    rules = Directory.GetAccessControl(path).GetAccessRules(true, true, typeof(SecurityIdentifier));
                }
                catch
                {
                    return true;
                }
                try
                {
                    foreach (FileSystemAccessRule rule in rules)
                    {
                        if (!current.User.Equals(rule.IdentityReference))
                        {
                            continue;
                        }
                        if (AccessControlType.Deny.Equals(rule.AccessControlType))
                        {
                            if ((FileSystemRights.Delete & rule.FileSystemRights) == FileSystemRights.Delete)
                                flag4 = true;
                            if ((FileSystemRights.Modify & rule.FileSystemRights) == FileSystemRights.Modify)
                                flag3 = true;
    
                            if ((FileSystemRights.Read & rule.FileSystemRights) == FileSystemRights.Read)
                                flag = true;
    
                            if ((FileSystemRights.Write & rule.FileSystemRights) == FileSystemRights.Write)
                                flag2 = true;
    
                            continue;
                        }
                        if (AccessControlType.Allow.Equals(rule.AccessControlType))
                        {
                            if ((FileSystemRights.Delete & rule.FileSystemRights) == FileSystemRights.Delete)
                            {
                                flag8 = true;
                            }
                            if ((FileSystemRights.Modify & rule.FileSystemRights) == FileSystemRights.Modify)
                            {
                                flag7 = true;
                            }
                            if ((FileSystemRights.Read & rule.FileSystemRights) == FileSystemRights.Read)
                            {
                                flag5 = true;
                            }
                            if ((FileSystemRights.Write & rule.FileSystemRights) == FileSystemRights.Write)
                            {
                                flag6 = true;
                            }
                        }
                    }
                    foreach (IdentityReference reference in current.Groups)
                    {
                        foreach (FileSystemAccessRule rule2 in rules)
                        {
                            if (!reference.Equals(rule2.IdentityReference))
                            {
                                continue;
                            }
                            if (AccessControlType.Deny.Equals(rule2.AccessControlType))
                            {
                                if ((FileSystemRights.Delete & rule2.FileSystemRights) == FileSystemRights.Delete)
                                    flag4 = true;
                                if ((FileSystemRights.Modify & rule2.FileSystemRights) == FileSystemRights.Modify)
                                    flag3 = true;
                                if ((FileSystemRights.Read & rule2.FileSystemRights) == FileSystemRights.Read)
                                    flag = true;
                                if ((FileSystemRights.Write & rule2.FileSystemRights) == FileSystemRights.Write)
                                    flag2 = true;
                                continue;
                            }
                            if (AccessControlType.Allow.Equals(rule2.AccessControlType))
                            {
                                if ((FileSystemRights.Delete & rule2.FileSystemRights) == FileSystemRights.Delete)
                                    flag8 = true;
                                if ((FileSystemRights.Modify & rule2.FileSystemRights) == FileSystemRights.Modify)
                                    flag7 = true;
                                if ((FileSystemRights.Read & rule2.FileSystemRights) == FileSystemRights.Read)
                                    flag5 = true;
                                if ((FileSystemRights.Write & rule2.FileSystemRights) == FileSystemRights.Write)
                                    flag6 = true;
                            }
                        }
                    }
                    bool flag9 = !flag4 && flag8;
                    bool flag10 = !flag3 && flag7;
                    bool flag11 = !flag && flag5;
                    bool flag12 = !flag2 && flag6;
                    bool flag13 = true;
                    if (checkRead)
                    {
                        //flag13 = flag13 && flag11;
                        flag13 = flag11;
                    }
                    if (checkWrite)
                    {
                        flag13 = flag13 && flag12;
                    }
                    if (checkModify)
                    {
                        flag13 = flag13 && flag10;
                    }
                    if (checkDelete)
                    {
                        flag13 = flag13 && flag9;
                    }
                    return flag13;
                }
                catch (IOException)
                {
                }
                return false;
            }
        }
    }

    谢谢浏览!

  • 相关阅读:
    使用SSH搭建简易的网上购物系统
    myeclipse8.0中安装maven插件m2eclipse
    Ant入门
    hibernate中多对多分解成一对多,
    在eclipse上,安装myeclipse插件
    hibernate复合主键的映射
    struts2中struts:iterator标签的使用
    hibernate复合主键同时做外键
    Mahout问题总括
    面向对象基础概念
  • 原文地址:https://www.cnblogs.com/Music/p/file-permission-helper-in-dotnet.html
Copyright © 2011-2022 走看看