C#添加文件和文件夹访问用户并设定权限
来源:互联网 发布:js 跳出foreach循环 编辑:程序博客网 时间:2024/06/06 15:02
源地址:http://www.cnblogs.com/rootkits/articles/1881101.html
其实在.NET中一切的操作和编程变的非常的简单而明了。如想要添加一个文件或文件夹访问用户并为其设置权限的话,如果在C++中实现则非常的复杂。并同时要调用那些烦人的API函数才能完成。但在.NET中则不同,因为.NET中用了很多已封装的类来完成。其实封装类的内部已经封装了系统的API函数从而解决了应用层的编程者。
以下是C#实现。用Visual Studio 2010编写,在WIN 7中测试通过。
1、文件
static void Main(string[] args)
{
SetAccount(@"C:\eee.txt", "BATCH");
Console.WriteLine("OK");
Console.Read();
}
public static void SetAccount(string filePath, string username)
{
FileInfo fileInfo = new FileInfo(filePath);
FileSecurity fileSecurity = fileInfo.GetAccessControl();
dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, AccessControlType.Allow)); //以完全控制为例
dirinfo.SetAccessControl(fileSecurity );
}
2、文件夹
public static void Main()
{
SetFolderACL("C:\\test", "BATCH", FileSystemRights.FullControl, AccessControlType.Allow);
}
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);
}
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;
}
例子: string path = Server.MapPath("~/Model/Organization/xml").Trim();//记得去空格
//设置xml文件夹可读写 tobey2011-02-18 add
SetFolderACLInfo.SetFolderACL(path, "Everyone", System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow);
- C#添加文件和文件夹访问用户并设定权限
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- sharepoint 2007 为独立权限的文件夹及其子文件和文件夹设置继承权限或者添加用户
- c#中如何设置文件夹权限和在安全中添加用户
- MySQL新建用户,及设定访问权限
- C# 设置文件夹访问权限
- mysql 添加用户访问权限
- mysql 添加用户访问权限
- mysql 添加用户访问权限
- mysql 添加用户访问权限(
- mysql 添加用户访问权限
- WIN2000系统文件访问权限默认设定。
- Centos6配置samba服务器并批量添加用户和文件夹
- 控制文件和文件夹的访问权限-FileIOPermission,CodeAccessPermission 类
- 图像降采样的实现详解
- 什么时候用css背景图,什么时候用img标签?
- [机器学习入门] 经典台大李宏毅机器学习课程从这里开始
- 归并排序(Mergesort)之Java实现
- python新手爬取论坛贴吧特定人的帖子——虎扑《健美大神之路》
- C#添加文件和文件夹访问用户并设定权限
- Bootstrap组件学习笔记(一)——大纲、Glyphicons字体图标和下拉菜单
- 新的更新主机文件的手段即将到来!
- LayUI tab 删除事件回调
- 设计模式之里氏转换原则
- 切换
- HTML5---01
- 训练赛第一场总结
- Apache启动 报Invalid command 'order', perhaps misspelled or defined by a module not included