我正在编写一个实用程序来帮助更改某个文件上的文件权限,以允许/禁止Windows计算机上的“每个人”组对其进行访问。 到目前为止,我已经能够通过使用以下代码设置并删除“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",AccessControlType.Deny)); File.SetAccessControl("file.tmp",fsFile); }
但是,我想检查“Everyone”是否已经具有“完全控制”权限,并且无法find执行此操作的方法。 在Googlesearch之后,我花了好几天的时间searchGooglesearch,并且无法find办法做到这一点。 有人能指引我正确的方向,或给我一个如何做到这一点的例子吗?
更新:这是非常迅速的答案,我能够想出可用的C#代码。 我创build的代码如下所示:
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"); } } } }
是否有可能通过关联的文件将命令行parameter passing给可执行文件?
与本地Windows服务进行通信的“正确”方式
使用.NET代码将系统时间同步到域控制器
使用Windows系统caching的.NET HttpRequests
我可以使用C#/ .NET以编程方式禁用窗口自动播放function吗?
.NET PInvoke在Linux和Mac OS X平台上可用吗?
使用适用于Windows XP / 7的Metro UI开发应用程序
.NET安全策略由标准用户更改?
在同一个应用程序中启动两个Windows服务
var everyone = fsFile.GetAccessRules(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);
如果权限可能同时包含Everyone和Allow条目,则必须使用类似以下的代码。 它有稍微不同的语义,因为你没有得到everyone Deny条目的细节。
var everyone = fsFile.GetAccessRules(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)
您必须获取该文件的授权规则,并检查是否有“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();
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,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); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。