[C#]修改文件权限、取消文件只读

        #region 修改文件权限
        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);
        }
        #endregion

        #region 取消文件只读
        static void SetFileAttributes(string aFilePath)
        {
            if (File.GetAttributes(aFilePath).ToString().IndexOf("ReadOnly") != -1)
            {
                File.SetAttributes(aFilePath, FileAttributes.Normal);
            }
        }
        #endregion

 

posted @ 2023-02-06 14:24  SairenjiHaruna  阅读(1084)  评论(0编辑  收藏  举报