zoukankan      html  css  js  c++  java
  • 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);
            }
    复制代码

    总结

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

  • 相关阅读:
    整理ASP.NET MVC 5各种错误请求[401,403,404,500]的拦截及自定义页面处理实例
    Sql Server 创建表添加说明
    c# 去除字符串中重复字符
    WebStorm中常用的快捷键及使用技巧
    git bash中的快捷键
    git bash here 的 ~/.bashrc 配置文件。和 vue/cli 3. 0 的 .vuerc文件(preset )
    右键菜单添加打开CMD选项
    Vue中使用Vue.component定义两个全局组件,用单标签应用组件时,只显示一个组件的问题和 $emit的使用。
    vue和stylus在subline中显示高亮
    stylus入门教程,在webstorm中配置stylus
  • 原文地址:https://www.cnblogs.com/soundcode/p/10861727.html
Copyright © 2011-2022 走看看