ルールがどこから発生したのかを見つけるには、ファイルまたはフォルダのパスを調べなければなりません。ここでは、すべてのアクセスルールとどこから由来したのかを表示する機能の粗いセットがあります。これを簡単に変更して、より便利なAPIを作成することができます(つまり、Consoleに印刷するだけでなく)。例えば
void PrintAccessRules(string path)
{
var security = File.GetAccessControl(path);
var accessRules = security.GetAccessRules(true, true, typeof(NTAccount));
foreach (var rule in accessRules.Cast<FileSystemAccessRule>())
{
if (!rule.IsInherited)
{
Console.WriteLine("{0} {1} to {2} was set on {3}.", rule.AccessControlType, rule.FileSystemRights, rule.IdentityReference, path);
continue;
}
FindInheritedFrom(rule, Directory.GetParent(path).FullName);
}
}
void FindInheritedFrom(FileSystemAccessRule rule, string path)
{
var security = File.GetAccessControl(path);
var accessRules = security.GetAccessRules(true, true, typeof(NTAccount));
var matching = accessRules.OfType<FileSystemAccessRule>()
.FirstOrDefault(r => r.AccessControlType == rule.AccessControlType && r.FileSystemRights == rule.FileSystemRights && r.IdentityReference == rule.IdentityReference);
if (matching != null)
{
if (matching.IsInherited) FindInheritedFrom(rule, Directory.GetParent(path).FullName);
else Console.WriteLine("{0} {1} to {2} is inherited from {3}", rule.AccessControlType, rule.FileSystemRights, rule.IdentityReference, path);
}
}
:
PrintAccessRules(@"C:\projects\mg\lib\repositories.config");
は私のために次のように出力されます
Allow FullControl to SkipTyler\Mike was set on C:\projects\mg\lib\repositories.config.
Allow ReadAndExecute, Synchronize to SkipTyler\Mike is inherited from C:\projects\mg
Allow FullControl to BUILTIN\Administrators is inherited from C:\
Allow FullControl to NT AUTHORITY\SYSTEM is inherited from C:\
Allow ReadAndExecute, Synchronize to BUILTIN\Users is inherited from C:\
あなたは、より効率的であるこのの任意のWindows APIのバージョン、知りませんか?私もそのことを考えましたが、たくさんのファイルを処理するとかなり高価なアルゴリズムです。 – Christoph
マネージコードには方法がありません。マネージコードはWinAPIを使用するだけなので、WinAPIへのストレートp/invokeを使用する方法があるかどうかはわかりません。 – HackedByChinese