最初に伝えたいのは、この解決策をどのように見つけたかです。ファイルのパーミッションを正しく取得するのは難しいため、これはおそらく答えよりも重要です。
最初に行ったのは、Windowsのダイアログとチェックボックスを使用して必要なアクセス許可を設定することでした。「Everyone」のルールを追加し、「FullControl」を除くすべてのボックスにチェックマークを付けました。
次に、このC#コードを記述して、Windows設定を複製するために必要なパラメーターを正確に教えてくれます。
string path = @"C:\Users\you\Desktop\perms"; // path to directory whose settings you have already correctly configured
DirectorySecurity sec = Directory.GetAccessControl(path);
foreach (FileSystemAccessRule acr in sec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount))) {
Console.WriteLine("{0} | {1} | {2} | {3} | {4}", acr.IdentityReference.Value, acr.FileSystemRights, acr.InheritanceFlags, acr.PropagationFlags, acr.AccessControlType);
}
これにより、次の出力行が得られました。
Everyone | Modify, Synchronize | ContainerInherit, ObjectInherit | None | Allow
したがって、解決策は単純です(何を探すべきかわからない場合でも、正しく理解するのは困難です!):
DirectorySecurity sec = Directory.GetAccessControl(path);
// Using this instead of the "Everyone" string means we work on non-English systems.
SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
Directory.SetAccessControl(path, sec);
これにより、Windowsのセキュリティダイアログのチェックボックスが、テストディレクトリにすでに設定されているものと一致するようになります。