コード例 #1
0
    public static void Main()
    {
        // Create a string representing the current user.
        string user = Environment.UserDomainName + "\\" +
                      Environment.UserName;

        // Create a security object that grants no access.
        SemaphoreSecurity mSec = new SemaphoreSecurity();

        // Add a rule that grants the current user the
        // right to enter or release the semaphore and read the
        // permissions on the semaphore.
        SemaphoreAccessRule rule = new SemaphoreAccessRule(user,
                                                           SemaphoreRights.Synchronize | SemaphoreRights.Modify
                                                           | SemaphoreRights.ReadPermissions,
                                                           AccessControlType.Allow);

        mSec.AddAccessRule(rule);

        // Add a rule that denies the current user the
        // right to change permissions on the semaphore.
        rule = new SemaphoreAccessRule(user,
                                       SemaphoreRights.ChangePermissions,
                                       AccessControlType.Deny);
        mSec.AddAccessRule(rule);

        // Display the rules in the security object.
        ShowSecurity(mSec);

        // Create a rule that grants the current user
        // the full control over the semaphore. Use the
        // SetAccessRule method to replace the
        // existing Allow rule with the new rule.
        rule = new SemaphoreAccessRule(user,
                                       SemaphoreRights.FullControl,
                                       AccessControlType.Allow);
        mSec.SetAccessRule(rule);

        ShowSecurity(mSec);
    }