zoukankan      html  css  js  c++  java
  • C#设置与获取目录权限(.net控制ACL)

    找到两种方式可以修改文件夹的权限

    第一种:

    想用c#来设置和读取ntfs分区上的目录权限,找了很多资料,未果。终于发现了一段vb.net的代码,做了修改,以C#展示给大家。

    using System;
    using System.Collections;
    using System.IO;
    using System.Security.AccessControl;
    static class Tester
    {

        public static void Main()
        {
            try
            {
                string filename = @"f:k"//目标目录
                string account = @"Administrator";//用户名
                string userrights = @"RW";//权限字符串,自己定义的
                AddDirectorySecurity(filename, account, userrights);
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Console.ReadLine();
            }
        }

        static public void AddDirectorySecurity(string FileName, string Account, string UserRights)
        {
            FileSystemRights Rights = new FileSystemRights();

            if (UserRights.IndexOf("R">= 0)
            {
                Rights = Rights | FileSystemRights.Read;
            }
            if (UserRights.IndexOf("C">= 0)
            {
                Rights = Rights | FileSystemRights.ChangePermissions;
            }
            if (UserRights.IndexOf("F">= 0)
            {
                Rights = Rights | FileSystemRights.FullControl;
            }
            if (UserRights.IndexOf("W">= 0)
            {
                Rights = Rights | FileSystemRights.Write;
            }

            bool ok;
            DirectoryInfo dInfo = new DirectoryInfo(FileName);
            DirectorySecurity dSecurity = dInfo.GetAccessControl();
            InheritanceFlags iFlags = new InheritanceFlags();
            iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
            FileSystemAccessRule AccessRule2 = new FileSystemAccessRule(Account, Rights, iFlags, PropagationFlags.None, AccessControlType.Allow);
            dSecurity.ModifyAccessRule(AccessControlModification.Add, AccessRule2, out ok);

            dInfo.SetAccessControl(dSecurity);

            //列出目标目录所具有的权限
            DirectorySecurity sec = Directory.GetAccessControl(FileName, AccessControlSections.All);
            foreach (FileSystemAccessRule rule in sec.GetAccessRules(truetruetypeof(System.Security.Principal.NTAccount)))
            {
                Console.WriteLine("----------------------------------");             Console.WriteLine(rule.IdentityReference.Value);             if ((rule.FileSystemRights & FileSystemRights.Read) != 0)                 Console.WriteLine(rule.FileSystemRights.ToString());         }         Console.Read();     } }



    对照MSDN,很容易看懂上面的代码。 但是貌似这个程序需要以管理员身份来运行。^_^

    其中的Directory.GetAccessControl(FileName, AccessControlSections.All);  第二个参数如果为AccessControlSections.Access ,就可以使得运行在IIS中的Web应用程序获得目录权限了。

    以上代码来源:http://www.cnblogs.com/zjneter/archive/2008/03/06/1093386.html

    第二种方法:该方法就不多说了,搜索大多博客都写了第二种方法来设置权限,但是,实际上这种方法根本无法给目录设置用户权限(测试多次无法设置成功,不知其他人是否成功设置),

            /// <summary>
            /// 目录权限
            /// </summary>
            public enum FloderRights
            {
                FullControl,
                Read,
                Write
            }
            public static void AddPathRights(string pathname, string username, FloderRights qx)
            {

                //FileInfo fi = new FileInfo(pathname);
                //System.Security.AccessControl.FileSecurity fileSecurity = fi.GetAccessControl();
                //fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
                //fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
                //fi.SetAccessControl(fileSecurity);


                DirectoryInfo dirinfo = new DirectoryInfo(Path.GetDirectoryName(pathname));
                if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)
                {
                    dirinfo.Attributes = FileAttributes.Normal;
                }
                //取得访问控制列表
                DirectorySecurity dirsecurity = dirinfo.GetAccessControl();
                // string strDomain = Dns.GetHostName();
                //System.Security.AccessControl.DirectorySecurity dirsecurity = dirinfo.GetAccessControl();
                switch (qx)
                {
                    case FloderRights.FullControl:
                        dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, AccessControlType.Allow));
                        break;
                    case FloderRights.Read:
                        dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow));
                        break;
                    case FloderRights.Write:
                        dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Write, AccessControlType.Allow));
                        break;
                    default:
                        dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, AccessControlType.Allow));
                        break;
                }
                dirinfo.SetAccessControl(dirsecurity);
                //System.IO.Directory.SetAccessControl(pathname, dirsecurity);

                //取消目录从父继承
                //DirectorySecurity dirSecurity = System.IO.Directory.GetAccessControl(pathname);
                //dirSecurity.SetAccessRuleProtection(true, false);
                //System.IO.Directory.SetAccessControl(pathname, dirSecurity);

                //AccessControlType.Allow允许访问受保护对象//Deny拒绝访问受保护对象
                //FullControl、Read 和 Write 完全控制,读,写
                //FileSystemRights.Write写入//Delete删除 //DeleteSubdirectoriesAndFiles删除文件夹和文件//ListDirectory读取
                //Modify读写删除-修改//只读打开文件和复制//
            }

  • 相关阅读:
    JDK路径下目录的介绍及环境变量配置
    Eclipse显示或隐藏项目
    linux 上传文件 rz命令 提示command not found 解决方法
    Query failed with error code 96 and error message 'Executor error during find command: OperationFailed: Sort operation used more than the maximum 33554432 bytes of RAM. Add an index, or specify a smal
    eclipse设置类和方的注释模板
    Xml 将一个Xml字节流写入到Dataset里面
    C# List<object> 转换为List<string>、转换为 string数组、转换为一个字符串用分隔符 隔开
    SQL 把m或f,更改为男女
    C# 去掉字符串里的分隔符(该分隔符可以是一个字符串)
    C# 通过一个开始时间和结束时间 按【AddDays】拆分成一个数组
  • 原文地址:https://www.cnblogs.com/xuxw/p/4410523.html
Copyright © 2011-2022 走看看