如何检查是否“所有人”具有完全控制权限的文件在c#

如何检查是否“所有人”具有完全控制权限的文件在c#,第1张

概述如何检查是否“所有人”具有完全控制权限的文件在c#

我正在编写一个实用程序来帮助更改某个文件上的文件权限,以允许/禁止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在linux上,〜/文件夹是错误的?

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

总结

以上是内存溢出为你收集整理的如何检查是否“所有人”具有完全控制权限的文件在c#全部内容,希望文章能够帮你解决如何检查是否“所有人”具有完全控制权限的文件在c#所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1293836.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存