C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限

写在前面

在windows系统中,c盘中的目录权限比较高,有时制作安装包的时候,默认的安装路径就是在c盘,但对运行可执行文件,有时候需要为其添加完全控制权限,或者读写权限。这里将当时的解决方案记录一下。

代码实现

在C盘添加一个文件夹,并在文件夹内部,新建一个文本文件,如图所示:

该文件夹下,新建一个文本文件,如图所示:

为文件添加完全控制权限:

复制代码
        /// <summary>
        /// 为文件添加users,everyone用户组的完全控制权限
        /// </summary>
        /// <param name="filePath"></param>
        static void AddSecurityControll2File(string filePath)
        {

            //获取文件信息
            FileInfo fileInfo = new FileInfo(filePath);
            //获得该文件的访问权限
            System.Security.AccessControl.FileSecurity fileSecurity = fileInfo.GetAccessControl();
            //添加ereryone用户组的访问权限规则 完全控制权限
            fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
            //添加Users用户组的访问权限规则 完全控制权限
            fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
            //设置访问权限
            fileInfo.SetAccessControl(fileSecurity);
        }
复制代码

为文件夹添加完全控制权限

复制代码
        /// <summary>
        ///为文件夹添加users,everyone用户组的完全控制权限
        /// </summary>
        /// <param name="dirPath"></param>
        static void AddSecurityControll2Folder(string dirPath)
        {
            //获取文件夹信息
            DirectoryInfo dir = new DirectoryInfo(dirPath);
            //获得该文件夹的所有访问权限
            System.Security.AccessControl.DirectorySecurity dirSecurity = dir.GetAccessControl(AccessControlSections.All);
            //设定文件ACL继承
            InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
            //添加ereryone用户组的访问权限规则 完全控制权限
            FileSystemAccessRule everyoneFileSystemAccessRule = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
            //添加Users用户组的访问权限规则 完全控制权限
            FileSystemAccessRule usersFileSystemAccessRule = new FileSystemAccessRule("Users", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
            bool isModified = false;
            dirSecurity.ModifyAccessRule(AccessControlModification.Add, everyoneFileSystemAccessRule, out isModified);
            dirSecurity.ModifyAccessRule(AccessControlModification.Add, usersFileSystemAccessRule, out isModified);
            //设置访问权限
            dir.SetAccessControl(dirSecurity);
        }
复制代码

总结

在操作文件的时候,还是比较简单的,不过文件夹就比较复杂了,牵扯到是否要继承的问题。


转自:http://www.cnblogs.com/wolf-sun/p/4591734.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在WinForms中实现用户权限控制可以使用代码实现。以下是一个基本的示例代码: ```csharp //定义用户角色和权限 enum UserRole { SuperAdmin, Admin, User } //定义功能列表和角色权限 Dictionary<string, UserRole[]> FunctionList = new Dictionary<string, UserRole[]> { {"AddUser", new UserRole[]{UserRole.SuperAdmin, UserRole.Admin}}, {"DeleteUser", new UserRole[]{UserRole.SuperAdmin, UserRole.Admin}}, {"ViewUser", new UserRole[]{UserRole.SuperAdmin, UserRole.Admin, UserRole.User}}, {"EditUser", new UserRole[]{UserRole.SuperAdmin, UserRole.Admin, UserRole.User}} }; //获取当前用户角色 UserRole GetCurrentUserRole() { //使用Windows身份验证机制验证当前用户身份 WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); //根据Windows账户组来确定当前用户角色 if (principal.IsInRole(WindowsBuiltInRole.Administrator)) { return UserRole.SuperAdmin; } else if (principal.IsInRole("Administrators")) { return UserRole.Admin; } else { return UserRole.User; } } //检查当前用户是否有执行该功能的权限 bool CheckUserPermission(string functionName) { //获取当前用户角色 UserRole currentUserRole = GetCurrentUserRole(); //获取该功能需要的角色权限 if (FunctionList.ContainsKey(functionName)) { UserRole[] allowedRoles = FunctionList[functionName]; //检查当前用户是否具有执行该功能的权限 if (Array.IndexOf(allowedRoles, currentUserRole) >= 0) { return true; } } return false; } //在按钮的Click事件中检查权限 private void btnAddUser_Click(object sender, EventArgs e) { //检查当前用户是否有执行该功能的权限 if (CheckUserPermission("AddUser")) { //执行该功能的代码 //... } else { //禁用按钮或者显示提示信息 //... } } ``` 以上示例代码定义了一个枚举类型`UserRole`来表示用户角色,一个字典类型`FunctionList`来表示功能列表和角色权限。在`GetCurrentUserRole()`方法中使用Windows身份验证机制来确定当前用户的角色,在`CheckUserPermission()`方法中检查当前用户是否具有执行该功能的权限。在按钮的`Click`事件中通过调用`CheckUserPermission()`方法来检查当前用户是否有执行该功能的权限。如果当前用户没有权限执行该功能,可以禁用按钮或者显示提示信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值