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

本文关键字:每个人 文件 权限 控制权 控制 检查 何检查 是否 | 更新日期: 2023-09-27 18:36:23

>我正在编写一个实用程序来帮助更改某个文件的文件权限,以允许/禁止Windows计算机上的"所有人"组访问该文件。 到目前为止,我已经能够使用以下代码设置和删除"每个人"对文件的完全控制权限:

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);
}

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

更新:这很快就得到了回答,我能够想出有效的 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");
            }
        }
    }
}

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

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);

如果权限可能包括 EveryoneAllow条目和Deny条目,则必须使用如下所示的代码。它的语义略有不同,因为您无法获得有关everyone Deny条目的详细信息。

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

您必须获取文件的授权规则,并检查是否有"所有人"帐户的规则。 然后,您可以检查规则的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();
一个

限制为"所有人"的文件,否则无法通过命令识别 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);
    }
}