向仅使用C#应用于文件的文件夹授予写权限

时间:2019-11-26 09:09:26

标签: c# directory-security

我正在使用以下方法向用户授予写权限,但是我想将“仅应用于”访问权限设置为“仅文件”。

注意:通过在文件夹属性>安全性的“高级安全设置”中双击用户,将出现“适用于:”选项。

问题:它会自动分配对“此文件夹,子文件夹和文件”的访问权限

期望:我想将其设置为“仅文件”

    private bool GrantAccess(string fullPath)
    {
    DirectoryInfo dInfo = new DirectoryInfo(fullPath);
    DirectorySecurity dSecurity = dInfo.GetAccessControl();
    string User = System.Environment.UserDomainName + "\\" + System.Environment.UserName;
    dSecurity.AddAccessRule(new FileSystemAccessRule(
        User, 
        FileSystemRights.Write,
        InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,
        PropagationFlags.NoPropagateInherit,
        AccessControlType.Allow));
        dInfo.SetAccessControl(dSecurity);
        return true;
    }

0 个答案:

没有答案