c#设置目录权限(.net控制alc)
来源:互联网 发布:qt高级编程 pdf下载 编辑:程序博客网 时间:2024/05/21 18:44
using System;
using System.Collections;
using System.IO;
using System.Security.AccessControl;
static class Tester
{
public static void Main()
{
try
{
string filename = @"d:/k"; //目标目录
string account = @"wch";//用户名
string userrights = @"R";//权限字符串,自己定义的
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();
}
}
- c#设置目录权限(.net控制alc)
- C#设置与获取目录权限(.net控制ACL)
- C#设置与获取目录权限(.net控制ACL)
- C#设置与获取目录权限(.net控制ACL)
- (证实可用)C#设置与获取目录权限(.net控制ACL)
- apache 设置目录权限控制
- C# 控制NTFS目录权限
- C# 控制NTFS目录权限
- asp.net目录权限设置图文综合
- web安全设置(含IIS,php,ASP.NET)与目录权限设置
- 度量快速开发平台部署IIS服务端后提示不具备查看该目录和页的权限 ALC
- svn 目录权限控制
- asp.net网站iis与目录权限设置
- RealTek瑞昱ALC声卡设置问题
- Linux目录权限设置
- apache目录权限设置
- IIS目录权限设置
- Tomcat目录权限设置
- MultiThread 2
- 谁动了我的Scancode?
- Flex 与 JavaScript相互调用函数
- web.xml和struts-config.xml配置方法
- 从两道经典试题谈C/C++中联合体(union)的使用
- c#设置目录权限(.net控制alc)
- c#汉字简体繁体转换的代码
- 你今天volatile了吗?--慎重使用
- 在内核模式驱动程序中调用 DLL
- 随机音乐程序
- 关于Debug和Release之本质区别的讨论(转载)
- gridview增加自动序号列
- Windows GDI和GDI+编程实例剖析(1)
- Best Windows Software