C# 添加一个用户对文件或者文件夹的所有权限

private void ModifyFilePermission(string path, string user, FileType filetype)

        {

            if (filetype == FileType.File)

            {

                FileInfo fileinfo = new FileInfo(path);

                FileSecurity fileSecurity = fileinfo.GetAccessControl();

                fileSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.FullControl, AccessControlType.Allow));

                fileinfo.SetAccessControl(fileSecurity);

            }

            else if (filetype == FileType.Directory)

            {

                DirectoryInfo di = new DirectoryInfo(path);

                DirectorySecurity dirSecurity = di.GetAccessControl();

                dirSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.FullControl, AccessControlType.Allow));

                di.SetAccessControl(dirSecurity);

            }

        }





        enum FileType

        {

            Directory,

            File

        }

 

你可能感兴趣的:(文件夹)