Example #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
        // ResetAccessRule method to replace both of
        // the existing rules with the new rule.
        rule = new SemaphoreAccessRule(user,
                                       SemaphoreRights.FullControl,
                                       AccessControlType.Allow);
        mSec.ResetAccessRule(rule);

        ShowSecurity(mSec);
    }