Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DirectorySecurity设置特殊权限,而FileSecurity不设置_C#_.net_Permissions_Implementation - Fatal编程技术网

C# DirectorySecurity设置特殊权限,而FileSecurity不设置

C# DirectorySecurity设置特殊权限,而FileSecurity不设置,c#,.net,permissions,implementation,C#,.net,Permissions,Implementation,检查以下两个代码块: System.Security.AccessControl.DirectorySecurity dsec = System.IO.Directory.GetAccessControl(str); System.Security.Principal.NTAccount group= new System.Security.Principal.NTAccount("DOMAIN","USERGROUP"); System.Security.AccessControl.FileS

检查以下两个代码块:

System.Security.AccessControl.DirectorySecurity dsec = System.IO.Directory.GetAccessControl(str);
System.Security.Principal.NTAccount group= new System.Security.Principal.NTAccount("DOMAIN","USERGROUP");
System.Security.AccessControl.FileSystemAccessRule myrule = new System.Security.AccessControl.FileSystemAccessRule(group,System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow);
dsec.SetAccessRule(myrule);
System.IO.Directory.SetAccessControl(str,dsec);

人们会期望它们都做完全相同的事情,只对一个目录和一个文件做一件事。而且,在某些方面,他们确实如此。在这两种情况下,所讨论的文件系统对象都会发生更改,使得DOMAIN\USERGROUP具有完全控制的有效权限

然而,奇怪的是,当您右键单击文件并查看安全性时,您会看到:

当您右键单击文件夹并查看安全性时,您会看到:

如果我接着进入高级->有效权限->选择(域\用户组),它会显示该组对文件夹的有效权限是完全控制(选中所有框,而不仅仅是完全控制框。这会更奇怪)


我的问题是,为什么几乎相同的实现的效果会有所不同?有人知道如何复制将权限应用到文件的效果吗?

在向文件添加权限方面,您可以尝试使用Logan

如果有帮助,请尝试此代码

    public static bool CheckReadWriteAccces(string filePath, System.Security.AccessControl.FileSystemRights fileSystemRights)
    {
        FileInfo fileInfo = new FileInfo(filePath);

        string str = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToUpper();
        foreach (System.Security.AccessControl.FileSystemAccessRule rule in fileInfo.GetAccessControl().GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
        {
            if (str == rule.IdentityReference.Value.ToUpper())
                return ((rule.AccessControlType == System.Security.AccessControl.AccessControlType.Allow) && (fileSystemRights == (rule.FileSystemRights & fileSystemRights)));
        }

        return false;
    }


    /// <summary>
    /// Make a file writteble
    /// </summary>
    /// <param name="path">File name to change</param>
    public static void MakeWritable(string path)
    {
        if (!File.Exists(path))
            return;
        File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly);
    }
public static bool checkreadwriteacces(字符串文件路径,System.Security.AccessControl.FileSystemRights FileSystemRights)
{
FileInfo FileInfo=新的FileInfo(filePath);
string str=System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToUpper();
foreach(fileInfo.GetAccessControl().GetAccessRules中的System.Security.AccessControl.FileSystemAccessRule规则(true、true、typeof(System.Security.Principal.NTAccount)))
{
if(str==rule.IdentityReference.Value.ToUpper())
返回((rule.AccessControlType==System.Security.AccessControl.AccessControlType.Allow)&&(fileSystemRights==(rule.fileSystemRights&fileSystemRights));
}
返回false;
}
/// 
///使文件可写
/// 
///要更改的文件名
公共静态void MakeWritable(字符串路径)
{
如果(!File.Exists(path))
返回;
SetAttributes(path、File.GetAttributes(path)&~FileAttributes.ReadOnly);
}

区别在于传播标志与目录安全性的相关性

var accessRule = new FileSystemAccessRule(
    identity: group,
    fileSystemRights: FileSystemRights.FullControl,
    type: AccessControlType.Allow,
    inheritanceFlags: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
    propagationFlags: PropagationFlags.None);

请注意
inheritanceFlags
设置。如果未指定,则默认值为“无”,分类为“特殊”。

Logan是否想要一个如何更改文件权限的示例?不,您误解了。我发布的代码有效。它只是对文件夹有一种奇怪的影响。我错了。。如果你不介意的话,我会把它作为一个更易读的版本,供那些想把文件权限作为学习参考的人使用。我不再能够对此进行测试或确认。我不再运行Windows,也不再在那个机构工作。不过,读这篇文章听起来不错,所以我会接受它,除非有人反驳。
var accessRule = new FileSystemAccessRule(
    identity: group,
    fileSystemRights: FileSystemRights.FullControl,
    type: AccessControlType.Allow,
    inheritanceFlags: InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
    propagationFlags: PropagationFlags.None);