C#设置与获取目录权限(.net控制ACL)
找到两种方式可以修改文件夹的权限
第一种:
想用c#来设置和读取ntfs分区上的目录权限,找了很多资料,未果。终于发现了一段vb.net的代码,做了修改,以C#展示给大家。




























































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)的更多相关文章
- WordPress 设置GeoIP数据库目录权限时错误解决方案
存在一个问题 更新完WP-statistics后,不知道为什么出现了一个错误提示:设置GeoIP数据库目录权限时错误,请确保您的Web服务器有权限写入到目录/var/www/html/wordpres ...
- vs开发 winform 设置winform 获取管理员权限启动
因为需要设置为开机项 没有管理员权限对注册表访问失败 C# 以管理员身份运行WinForm程序 转载https://www.bbsmax.com/A/obzbkKrQJE/ 鱼洛 2016-07-29 ...
- ssh keygen命令实现免密码通信(git库获取操作权限:开发人员添加到git库中,获取操作权限)
先看两个机器实现免密码登陆通讯: 假设 A 为客户机器,B为目标机: 要达到的目的: A机器ssh登录B机器无需输入密码: 加密方式选 rsa|dsa均可以,默认dsa 做法: 1.登录A机器 2.s ...
- [转]正确设置nginx/php-fpm/apache权限
核心总结:php-fpm/apache 进程所使用的用户,不能是网站文件所有者. 凡是违背这个原则,则不符合最小权限原则. 根据生产环境不断反馈,发现不断有 php网站被挂木马,绝大部分原因是因为权限 ...
- 正确设置nginx/php-fpm/apache权限 提高网站安全性 防止被挂木马
核心总结:php-fpm/apache 进程所使用的用户,不能是网站文件所有者. 凡是违背这个原则,则不符合最小权限原则. 根据生产环境不断反馈,发现不断有 php网站被挂木马,绝大部分原因是因为权限 ...
- Nginx设置alias别名目录访问phpmyadmin
引言:Nginx服务器通过设置alias别名可以使特定的目录(phpmyadmin目录)不出现在网站根目录下面,即使网站根目录被攻破,也不会影响到phpmyadmin目录里面的文件. 说明: 站点:h ...
- 网卡配置文件详解 用户管理与文件权限篇 文件与目录权限 软连接 tar解压命令 killall命令 linux防火墙 dns解析设置 计划任务crond服务 软件包安装 阿里云 yum源 安装
Linux系统基础优化及常用命令 Linux基础系统优化 引言没有,只有一张图. Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. ...
- phalcon: 目录分组后的acl权限控制
phalcon: 目录分组后的acl权限控制 楼主在做acl权限的时候,发现官方的acl只能针对未分组的目录,如下: app/ ___|./controller ___|./logic ___|./p ...
- 获取当前目录getcwd,设置工作目录chdir,获取目录信息
#include <unistd.h> #include <stdio.h> #include <limits.h> int main(int argc, char ...
随机推荐
- XSS跨站脚本攻击在Java开发中防范的方法
1. 防堵跨站漏洞,阻止攻击者利用在被攻击网站上发布跨站攻击语句不可以信任用户提交的任何内容,首先代码里对用户输入的地方和变量都需要仔细检查长度和对”<”,”>”,”;”,”’”等字符做过 ...
- IIS出现Server Error in '/' Application.CS0016的解决办法
这两天一直在弄IIS的事,全都是报错,网上找了好多资料,也尝试了很多,终于在几分钟之前把困扰了我一周的麻烦给解决了,现整理出来,希望对大家有用,闲话少说,直接上图了 Server Error in ' ...
- [CODEVS3641]上帝选人
题目描述 Description 世界上的人都有智商IQ和情商EQ.我们用两个数字来表示人的智商和情商,数字大就代表其相应智商或情商高.现在你面前有N个人,这N个人的智商和情商均已知,请你选择出尽量多 ...
- Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 554 Solved: 346[ ...
- bayer图像格式
Bayer数据,其一般格式为:奇数扫描行输出 RGRG……偶数扫描行输出 GBGB…… 根据人眼对彩色的响应带宽不高的大面积着色特点,每个像素没有必要同时输出3种颜色.因此,数据采样时, 奇数扫描行的 ...
- Stand-up meeting
A stand-up meeting (or simply "stand-up") is a daily team-meeting held to provide a status ...
- curl测试puppet http api接口
---恢复内容开始--- 基于证书密钥对 curl --cert /etc/puppetlabs/puppet/ssl/certs/master.puppet.org.pem --key /etc/p ...
- 关于升级linux下apache
1:Check whether Apache is already installed. # rpm -qa | grep -i http httpd-tools-2.2.8-3.i386 httpd ...
- spf13配置问题
git clone https://github.com/spf13/spf13-vim.git tab是黑的色 加入这样一行就可以了set nolist if下的黑边在~/.vimrc里面吧inde ...
- NIO机制总结
Selector selector = Selector.open(); 普通的IO流的读取,写入都是一个字节一个字节或一个字符一个字符的循环进行,在这个过程中,程序是阻塞的,inputStream虽 ...