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;
            }
        }
    }

    谢谢浏览!

  • 相关阅读:
    【转】前端防止 JS 调试技巧
    反爬虫 js怎样判断是真实点击事件还是模拟点击事件?
    js 前端 滑动验证
    【转】pyspider运行卡死在result_worker starting 的解决办法
    【转】pyspider all命令报错如下:ImportError: cannot import name 'DispatcherMiddleware' from 'werkzeug.wsgi'
    【转】pyspider中async关键字问题
    【转】Windows python3.7 下安装运行pyspider
    如何修改11g RAC集群名称
    Exadata健康检查工具EXAchk
    XD刷机中执行reclaimdisks.sh的作用
  • 原文地址:https://www.cnblogs.com/Music/p/file-permission-helper-in-dotnet.html
Copyright © 2011-2022 走看看