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(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)的更多相关文章
- 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 ...
随机推荐
- 改善C#程序的50种方法
为什么程序已经可以正常工作了,我们还要改变它们呢?答案就是我们可以让它们变得更好.我们常常会改变所使用的工具或者语言,因为新的工具或者语言更富生产力.如果固守旧有的习惯,我们将得不到期望的结果.对于C ...
- Custom ASP.NET Application into SharePoint --整合ASP.NET应用程序到SharePoint
转:http://www.devexpertise.com/2009/02/18/integrating-a-custom-aspnet-application-into-sharepoint-par ...
- JS日期时间格式化
Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + ...
- HDU-2562 奇偶位互换
http://acm.hdu.edu.cn/showproblem.php?pid=2562 奇偶位互换 Time Limit: 3000/1000 MS (Java/Others) Memor ...
- ASCII,Unicode和UTF-8字符编码
ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(byte).也就是 ...
- 在net安装程序中部署oracle客户端全攻略
在net安装程序中部署oracle客户端全攻略 主要的是要做三件工作: 打包文件,写注册表,注册环境变量说明:我的oracle版本为9, 在2000 advanced server 上测试通过,可以正 ...
- [NOIP2005]采药
2005年NOIP全国联赛普及组 [题目描述 Description] 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个 ...
- Mac下快捷键列表
主界面 command + tab 切换程序 command + ` 在程序内切换界面 command + w 关闭界面 command + q 关闭程序 command + option + esc ...
- 神经网络中误差反向传播(back propagation)算法的工作原理
注意:版权所有,转载需注明出处. 神经网络,从大学时候就知道,后面上课的时候老师也讲过,但是感觉从来没有真正掌握,总是似是而非,比较模糊,好像懂,其实并不懂. 在开始推导之前,需要先做一些准备工作,推 ...
- [bzoj\lydsy\大视野在线测评]题解(持续更新)
目录: 一.DP 二.图论 1.最短路 2.强连通分量 三.利用单调性维护 四.贪心 五.数据结构 1.并查集 六.数学 1.计数问题 2.数学分析 七.博弈 八.搜索 /////////////// ...