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

第一种:

想用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(true, true, typeof(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读写删除-修改//只读打开文件和复制//
        }

C#设置与获取目录权限(.net控制ACL)的更多相关文章

  1. WordPress 设置GeoIP数据库目录权限时错误解决方案

    存在一个问题 更新完WP-statistics后,不知道为什么出现了一个错误提示:设置GeoIP数据库目录权限时错误,请确保您的Web服务器有权限写入到目录/var/www/html/wordpres ...

  2. vs开发 winform 设置winform 获取管理员权限启动

    因为需要设置为开机项 没有管理员权限对注册表访问失败 C# 以管理员身份运行WinForm程序 转载https://www.bbsmax.com/A/obzbkKrQJE/ 鱼洛 2016-07-29 ...

  3. ssh keygen命令实现免密码通信(git库获取操作权限:开发人员添加到git库中,获取操作权限)

    先看两个机器实现免密码登陆通讯: 假设 A 为客户机器,B为目标机: 要达到的目的: A机器ssh登录B机器无需输入密码: 加密方式选 rsa|dsa均可以,默认dsa 做法: 1.登录A机器 2.s ...

  4. [转]正确设置nginx/php-fpm/apache权限

    核心总结:php-fpm/apache 进程所使用的用户,不能是网站文件所有者. 凡是违背这个原则,则不符合最小权限原则. 根据生产环境不断反馈,发现不断有 php网站被挂木马,绝大部分原因是因为权限 ...

  5. 正确设置nginx/php-fpm/apache权限 提高网站安全性 防止被挂木马

    核心总结:php-fpm/apache 进程所使用的用户,不能是网站文件所有者. 凡是违背这个原则,则不符合最小权限原则. 根据生产环境不断反馈,发现不断有 php网站被挂木马,绝大部分原因是因为权限 ...

  6. Nginx设置alias别名目录访问phpmyadmin

    引言:Nginx服务器通过设置alias别名可以使特定的目录(phpmyadmin目录)不出现在网站根目录下面,即使网站根目录被攻破,也不会影响到phpmyadmin目录里面的文件. 说明: 站点:h ...

  7. 网卡配置文件详解 用户管理与文件权限篇 文件与目录权限 软连接 tar解压命令 killall命令 linux防火墙 dns解析设置 计划任务crond服务 软件包安装 阿里云 yum源 安装

    Linux系统基础优化及常用命令 Linux基础系统优化 引言没有,只有一张图. Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. ...

  8. phalcon: 目录分组后的acl权限控制

    phalcon: 目录分组后的acl权限控制 楼主在做acl权限的时候,发现官方的acl只能针对未分组的目录,如下: app/ ___|./controller ___|./logic ___|./p ...

  9. 获取当前目录getcwd,设置工作目录chdir,获取目录信息

    #include <unistd.h> #include <stdio.h> #include <limits.h> int main(int argc, char ...

随机推荐

  1. 【HDOJ】2851 Lode Runner

    开始没理解题意.原来destinations是指路的序号.而不是点.DP. #include <stdio.h> #include <string.h> ]; typedef ...

  2. 【HDOJ】2159 FATE

    DP+贪心优化. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXNUM ...

  3. C++ Prime:const的引用

    可以把引用绑定到const对象上,就像绑定到其他对象上一样,我们称之为对常量的引用.与普通引用不同的是,对常量的引用不能被用作修改它所绑定的对象: ; const int &r1 = ci; ...

  4. C#面向对象——方法的重载及构造函数、静态对象。

    namespace nameclass2 { class Class2 //方法的重载 { public void Function( string s) { Console.WriteLine(s) ...

  5. 比较详细的利用虚拟机对SD卡FAT32+EXT4+Ext4分区图解教程

    如果大家嫌虚拟机复杂,我这里提供一个我没用虚拟机之前的分区方法:这个方法实际是可行的 我在没有用虚拟机之前,我是这样操作的1.首先在分区软件分好fat32+ext2+ext22.然后用recovery ...

  6. java基础(二十一)IO流(四)

    这里有我之前上课总结的一些知识点以及代码大部分是老师讲的笔记 个人认为是非常好的,,也是比较经典的内容,真诚的希望这些对于那些想学习的人有所帮助! 由于代码是分模块的上传非常的不便.也比较多,讲的也是 ...

  7. [GRYZ2014]最大连续子序列的和

    求给定序列的最大连续子序列和. 输入:第一行:n(n<100000)             第二行:n个整数[-3000,3000]. 输出:最大连续子序列的和. 样例: 输入: 7 -6 4 ...

  8. 洛谷1373 小a和uim之大逃离

    洛谷1373 小a和uim之大逃离 本题地址:http://www.luogu.org/problem/show?pid=1373 题目背景 小a和uim来到雨林中探险.突然一阵北风吹来,一片乌云从北 ...

  9. yii快速入门与参考

    Yii 权威指南 http://www.yiichina.com/guide/ http://www.yiiframework.com/doc/guide/1.1/zh_cn/ Ⅰ.基本概念一.入口文 ...

  10. LCS算法

    转自:http://hzzy-010.blog.163.com/blog/static/79692381200872024242126/  好详细~~~也十分好理解~~~ 最长公共子序列问题(非连续的 ...