.Net下查看和修改文件夹的ACL安全权限(C#)
2011-01-05 09:33
821 查看
http://blog.csdn.net/hz932/archive/2008/07/12/2644097.aspx
这是一个在.Net下修改文件夹或文件的ACL安全权限的类:
SetFolderACL:两个重载函数,设置权限的方法,根据需要选择重载。
GetACL: 查看文件夹权限的信息,用户名-权限键值对
GetACLString:查看文件夹权限的文本信息,用户名-权限名键值对
view plaincopy to clipboardprint?
using System;
using System.Collections;
using System.Text;
using System.Security.AccessControl;
using System.IO;
view plaincopy to clipboardprint?
namespace ACL
{
class ACL_FS
{ //By 同济黄正 <A href="http://hz932.ys168.com">http://hz932.ys168.com</A>
public static bool SetFolderACL(String FolderPath , String UserName , FileSystemRights Rights , AccessControlType AllowOrDeny)
{
InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
return SetFolderACL(FolderPath , UserName , Rights , AllowOrDeny , inherits , PropagationFlags.None , AccessControlModification.Add);
}
view plaincopy to clipboardprint?
public static bool SetFolderACL(String FolderPath , String UserName , FileSystemRights Rights , AccessControlType AllowOrDeny
, InheritanceFlags Inherits , PropagationFlags PropagateToChildren , AccessControlModification AddResetOrRemove)
{
//过程:获取文件夹安全对象、构造访问规则、修改安全对象的访问规则、重新设置文件夹安全对象
bool ret;
DirectoryInfo folder = new DirectoryInfo(FolderPath);
DirectorySecurity dSecurity = folder.GetAccessControl(AccessControlSections.All);
FileSystemAccessRule accRule = new FileSystemAccessRule(UserName , Rights , Inherits , PropagateToChildren , AllowOrDeny);
dSecurity.ModifyAccessRule(AddResetOrRemove , accRule , out ret);
folder.SetAccessControl(dSecurity);
return ret;
}
view plaincopy to clipboardprint?
/// <returns>String,FileSystemRights键值对</returns>
public static Hashtable GetACL(String FolderPath)
{
Hashtable ret = new Hashtable();
DirectorySecurity sec = Directory.GetAccessControl(FolderPath , AccessControlSections.All);
foreach (FileSystemAccessRule rule in sec.GetAccessRules(true , true , typeof(System.Security.Principal.NTAccount)))
{
ret[rule.IdentityReference.ToString()] = rule.FileSystemRights;
}
return ret;
}
public static string GetACLString(String FolderPath)
{
StringBuilder sb = new StringBuilder();
Hashtable rights=GetACL(FolderPath);
foreach (string key in rights.Keys)
{
sb.Append(key + ":/t" + ((FileSystemRights)rights[key]).ToString()+"/r/n");
}
return sb.ToString();
}
view plaincopy to clipboardprint?
}
}
//以上在WindowsXP、Windows Server 2003下测试通过。
这是一个在.Net下修改文件夹或文件的ACL安全权限的类:
SetFolderACL:两个重载函数,设置权限的方法,根据需要选择重载。
GetACL: 查看文件夹权限的信息,用户名-权限键值对
GetACLString:查看文件夹权限的文本信息,用户名-权限名键值对
view plaincopy to clipboardprint?
using System;
using System.Collections;
using System.Text;
using System.Security.AccessControl;
using System.IO;
using System; using System.Collections; using System.Text; using System.Security.AccessControl; using System.IO;
view plaincopy to clipboardprint?
namespace ACL
{
class ACL_FS
{ //By 同济黄正 <A href="http://hz932.ys168.com">http://hz932.ys168.com</A>
public static bool SetFolderACL(String FolderPath , String UserName , FileSystemRights Rights , AccessControlType AllowOrDeny)
{
InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
return SetFolderACL(FolderPath , UserName , Rights , AllowOrDeny , inherits , PropagationFlags.None , AccessControlModification.Add);
}
namespace ACL { class ACL_FS { //By 同济黄正 http://hz932.ys168.com public static bool SetFolderACL(String FolderPath , String UserName , FileSystemRights Rights , AccessControlType AllowOrDeny) { InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit; return SetFolderACL(FolderPath , UserName , Rights , AllowOrDeny , inherits , PropagationFlags.None , AccessControlModification.Add); }
view plaincopy to clipboardprint?
public static bool SetFolderACL(String FolderPath , String UserName , FileSystemRights Rights , AccessControlType AllowOrDeny
, InheritanceFlags Inherits , PropagationFlags PropagateToChildren , AccessControlModification AddResetOrRemove)
{
//过程:获取文件夹安全对象、构造访问规则、修改安全对象的访问规则、重新设置文件夹安全对象
bool ret;
DirectoryInfo folder = new DirectoryInfo(FolderPath);
DirectorySecurity dSecurity = folder.GetAccessControl(AccessControlSections.All);
FileSystemAccessRule accRule = new FileSystemAccessRule(UserName , Rights , Inherits , PropagateToChildren , AllowOrDeny);
dSecurity.ModifyAccessRule(AddResetOrRemove , accRule , out ret);
folder.SetAccessControl(dSecurity);
return ret;
}
public static bool SetFolderACL(String FolderPath , String UserName , FileSystemRights Rights , AccessControlType AllowOrDeny , InheritanceFlags Inherits , PropagationFlags PropagateToChildren , AccessControlModification AddResetOrRemove) { //过程:获取文件夹安全对象、构造访问规则、修改安全对象的访问规则、重新设置文件夹安全对象 bool ret; DirectoryInfo folder = new DirectoryInfo(FolderPath); DirectorySecurity dSecurity = folder.GetAccessControl(AccessControlSections.All); FileSystemAccessRule accRule = new FileSystemAccessRule(UserName , Rights , Inherits , PropagateToChildren , AllowOrDeny); dSecurity.ModifyAccessRule(AddResetOrRemove , accRule , out ret); folder.SetAccessControl(dSecurity); return ret; }
view plaincopy to clipboardprint?
/// <returns>String,FileSystemRights键值对</returns>
public static Hashtable GetACL(String FolderPath)
{
Hashtable ret = new Hashtable();
DirectorySecurity sec = Directory.GetAccessControl(FolderPath , AccessControlSections.All);
foreach (FileSystemAccessRule rule in sec.GetAccessRules(true , true , typeof(System.Security.Principal.NTAccount)))
{
ret[rule.IdentityReference.ToString()] = rule.FileSystemRights;
}
return ret;
}
public static string GetACLString(String FolderPath)
{
StringBuilder sb = new StringBuilder();
Hashtable rights=GetACL(FolderPath);
foreach (string key in rights.Keys)
{
sb.Append(key + ":/t" + ((FileSystemRights)rights[key]).ToString()+"/r/n");
}
return sb.ToString();
}
/// <returns>String,FileSystemRights键值对</returns> public static Hashtable GetACL(String FolderPath) { Hashtable ret = new Hashtable(); DirectorySecurity sec = Directory.GetAccessControl(FolderPath , AccessControlSections.All); foreach (FileSystemAccessRule rule in sec.GetAccessRules(true , true , typeof(System.Security.Principal.NTAccount))) { ret[rule.IdentityReference.ToString()] = rule.FileSystemRights; } return ret; } public static string GetACLString(String FolderPath) { StringBuilder sb = new StringBuilder(); Hashtable rights=GetACL(FolderPath); foreach (string key in rights.Keys) { sb.Append(key + ":/t" + ((FileSystemRights)rights[key]).ToString()+"/r/n"); } return sb.ToString(); }
view plaincopy to clipboardprint?
}
}
//以上在WindowsXP、Windows Server 2003下测试通过。
} } //以上在WindowsXP、Windows Server 2003下测试通过。
相关文章推荐
- .Net下查看和修改文件夹的ACL安全权限(C#)
- .Net下修改文件夹或文件的ACL安全权限
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- win7系统如何批量修改文件和文件夹权限右键没有安全选项卡
- c#.net 更改文件夹权限
- 查看修改文件与文件夹权限
- C#删除单个文件或文件夹(权限修改)
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- c# .net 监听磁盘映射 变更,及文件夹权限2
- ASP.NET C#.NET 通用权限管理系统组件2011年01月BUG修改情况列表清单
- ASP.NET C#.NET 通用权限管理系统组件2011年01月BUG修改情况列表清单
- c# .net 监听磁盘映射 变更,及文件夹权限1
- 指令篇:文件和文件夹权限查看和修改___chmod、chown、chgrp、file
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- C#删除单个文件或文件夹(权限修改)
- 用 C# 给文件夹设置 NTFS 权限(ACL)