如何检查“Everyone”是否对c#中的文件具有完全控制权限

时间:2013-10-22 18:23:32

标签: c# .net windows

我正在编写一个实用程序来帮助更改某个文件的文件权限,以允许/禁止对Windows机器上的“Everyone”组进行访问。到目前为止,我已经能够使用以下代码设置和删除“Everyone”的完全控制权限:

void AddFullControl()
{
    FileSecurity fsFile = File.GetAccessControl("file.tmp");
    fsFile.SetAccessRule( new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
    File.SetAccessControl("file.tmp", fsFile);
}

void RemoveFullControl()
{
    FileSecurity fsFile = File.GetAccessControl("file.tmp");
    fsFile.SetAccessRule( new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Deny));
    File.SetAccessControl("file.tmp", fsFile);
}

但是,我想检查一下“Everyone”是否已经拥有完全控制权限,并且无法找到执行此操作的方法。我在谷歌搜索后花了几天时间搜索谷歌搜索,但却找不到办法。有人可以指出我正确的方向或给我一个如何做到这一点的例子吗?

更新 这很快得到了解答,我能够提出有效的c#代码。我创建的代码如下:

void CheckAccess()
{
    AuthorizationRuleCollection arcFile = File.GetAccessControl("file.tmp").GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
    foreach (AuthorizationRule arFile in arcFile)
    {
        if (arFile.IdentityReference.Value == "Everyone")
        {
            FileSystemAccessRule fasrFile = (FileSystemAccessRule)arFile;
            if (fasrFile.AccessControlType == AccessControlType.Allow && fasrFile.FileSystemRights.HasFlag(FileSystemRights.FullControl))
            {
                MessageBox.Show("file.tmp already has Full Control permissions granted to Everyone");
            }
        }
    }
}

3 个答案:

答案 0 :(得分:6)

var everyone = fsFile.GetAccessRules(true, true, typeof(SecurityIdentifier))
    .Cast<FileSystemAccessRule>()
    .SingleOrDefault(x => x.IdentityReference.Value == "S-1-1-0");
bool fullControlAllowed = everyone != null
             && everyone.AccessControlType == AccessControlType.Allow
             && everyone.FileSystemRights.HasFlag(FileSystemRights.FullControl);

如果权限可能同时包含Allow的{​​{1}}和Deny条目,则必须使用以下代码。它的语义略有不同,因为您无法获得Everyone everyone条目的详细信息。

Deny

答案 1 :(得分:3)

您必须获取该文件的授权规则,并检查“Everyone”帐户是否有规则。然后,您可以查看规则的FileSystemRights,看看它是否有FullControl

var account = @"Everyone";
var hasFullControl = rules.OfType<FileSystemAccessRule>()
    .Where(rule => rule.IdentityReference.Value == account && rule.AccessControlType == AccessControlType.Allow)
    .Select(rule => (bool?)rule.FileSystemRights.HasFlag(FileSystemRights.FullControl))
    .SingleOrDefault();

答案 2 :(得分:0)

限制为“Everyone”的文件,否则无法通过命令if(Directory.Exists(pathfile))识别,因为该文件受访问保护,编译器无法识别它在指定目录中的存在,并且它将始终点击!Directory.Exists(pathfile)命令。 如果你想每次都写新数据,那么这可能有所帮助,

string pathfile = @"C:\\Users\\Public\\Documents\\Filepath.txt";
if (!Directory.Exists(pathfile))
{
    File.SetAttributes(pathfile, FileAttributes.Normal);
    File.Delete(pathfile);

    using (FileStream fs = File.Create(pathfile))
    {
        Byte[] info = new UTF8Encoding(true).GetBytes("What Ever Your Text is");

        fs.Write(info, 0, info.Length);
        File.SetAttributes(pathfile, FileAttributes.ReadOnly);
        FileSecurity fsec = File.GetAccessControl(pathfile);
        fsec.AddAccessRule(new FileSystemAccessRule("Everyone",
        FileSystemRights.ReadData, AccessControlType.Allow));
        File.SetAccessControl(pathfile, fsec);
    }
}
相关问题