Export (0) Print
Expand All

UIPermission Class

Controls the permissions related to user interfaces and the clipboard. This class cannot be inherited.

Namespace: System.Security.Permissions
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class UIPermission : CodeAccessPermission, IUnrestrictedPermission
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class UIPermission extends CodeAccessPermission implements IUnrestrictedPermission
SerializableAttribute 
ComVisibleAttribute(true) 
public final class UIPermission extends CodeAccessPermission implements IUnrestrictedPermission
Not applicable.

Drawing and user input events in windows are user interfaces.

The permission to use windows can be one of the following: unrestricted, limited to SafeTopLevelWindows, only SafeSubWindows, or no window drawing or user input event access allowed. SafeTopLevelWindows and SafeSubWindows are restricted in title and size to prevent possible spoofing by potentially harmful code.

The permission to use the clipboard can be one of the following: unrestricted, write-only, or no clipboard access allowed. The paste limitation prevents potentially harmful applications from taking data from the clipboard without the user's consent, while still allowing the cut, copy, and paste operations when initiated by the user through keyboard commands.

The following code example shows the behavior of the UIPermission class methods.

NoteNote:

The code example is intended to show the behavior of the methods, not to demonstrate their use. In general, the methods of permission classes are used by the security infrastructure; they are not typically used in applications. Generally, only the constructors are used in application code. The created instance validates or controls resource access by using inherited CodeAccessPermission methods such as Demand.

using System;
using System.Security;
using System.Security.Permissions;

public class UIPermissionDemo
{

    public static void Main(String[] args)
    {
        IsSubsetOfDemo();
        CopyDemo();
        UnionDemo();
        IntersectDemo();
        ToFromXmlDemo();
    }

    // IsSubsetOf determines whether the current permission is a subset of the specified permission.
    private static void IsSubsetOfDemo()
    {
        UIPermission uiPerm1 = new UIPermission(UIPermissionWindow.SafeTopLevelWindows);
        UIPermission uiPerm2 = new UIPermission(UIPermissionWindow.SafeSubWindows);
        CheckIsSubsetOfWindow(uiPerm1, uiPerm2);
        uiPerm1 = new UIPermission(UIPermissionClipboard.AllClipboard);
        uiPerm2 = new UIPermission(UIPermissionClipboard.OwnClipboard);
        CheckIsSubsetOfClipBoard(uiPerm1, uiPerm2);
    }
    private static void CheckIsSubsetOfWindow(UIPermission uiPerm1, UIPermission uiPerm2)
    {
        if (uiPerm1.IsSubsetOf(uiPerm2))
        {
            Console.WriteLine(uiPerm1.Window.ToString() + " is a subset of " +
                uiPerm2.Window.ToString());
        }
        else
        {
            Console.WriteLine(uiPerm1.Window.ToString() + " is not a subset of " +
                uiPerm2.Window.ToString());

        }
        if (uiPerm2.IsSubsetOf(uiPerm1))
        {
            Console.WriteLine(uiPerm2.Window.ToString() + " is a subset of " +
                uiPerm1.Window.ToString());
        }
        else
        {
            Console.WriteLine(uiPerm2.Window.ToString() + " is not a subset of " +
                uiPerm1.Window.ToString());

        }
    }
    private static void CheckIsSubsetOfClipBoard(UIPermission uiPerm1, UIPermission uiPerm2)
    {
        if (uiPerm1.IsSubsetOf(uiPerm2))
        {
            Console.WriteLine(uiPerm1.Clipboard.ToString() + " is a subset of " +
                uiPerm2.Clipboard.ToString());
        }
        else
        {
            Console.WriteLine(uiPerm1.Clipboard.ToString() + " is not a subset of " +
                uiPerm2.Clipboard.ToString());

        }
        if (uiPerm2.IsSubsetOf(uiPerm1))
        {
            Console.WriteLine(uiPerm2.Clipboard.ToString() + " is a subset of " +
                uiPerm1.Clipboard.ToString());
        }
        else
        {
            Console.WriteLine(uiPerm2.Clipboard.ToString() + " is not a subset of " +
                uiPerm1.Clipboard.ToString());

        }
    }
    // Union creates a new permission that is the union of the current permission
    // and the specified permission.
    private static void UnionDemo()
    {
        UIPermission uiPerm1 = new UIPermission(UIPermissionWindow.SafeTopLevelWindows);
        UIPermission uiPerm2 = new UIPermission(UIPermissionWindow.SafeSubWindows);
        UIPermission p3 = (UIPermission)uiPerm1.Union(uiPerm2);
        try
        {
            if (p3 != null)
            {
                Console.WriteLine("The union of " + uiPerm1.Window.ToString() +
                    " and \n\t" + uiPerm2.Window.ToString() + " is \n\t"
                    + p3.Window.ToString() + "\n");

            }
            else
            {
                Console.WriteLine("The union of " + uiPerm1.Window.ToString() +
                    " and \n\t" + uiPerm2.Window.ToString() + " is null.\n");
            }
        }
        catch (SystemException e)
        {
            Console.WriteLine("The union of " + uiPerm1.Window.ToString() +
                    " and \n\t" + uiPerm2.Window.ToString() + " failed.");

            Console.WriteLine(e.Message);
        }

    }
    // Intersect creates and returns a new permission that is the intersection of the
    // current permission and the permission specified.
    private static void IntersectDemo()
    {
        UIPermission uiPerm1 = new UIPermission(UIPermissionWindow.SafeTopLevelWindows, UIPermissionClipboard.OwnClipboard);
        UIPermission uiPerm2 = new UIPermission(UIPermissionWindow.SafeSubWindows, UIPermissionClipboard.NoClipboard);
        UIPermission p3 = (UIPermission)uiPerm1.Intersect(uiPerm2);

        Console.WriteLine("The intersection of " + uiPerm1.Window.ToString() + " and \n\t" +
            uiPerm2.Window.ToString() + " is " + p3.Window.ToString() + "\n");
        Console.WriteLine("The intersection of " + uiPerm1.Clipboard.ToString() + " and \n\t" +
                uiPerm2.Clipboard.ToString() + " is " + p3.Clipboard.ToString() + "\n");

    }
    //Copy creates and returns an identical copy of the current permission.
    private static void CopyDemo()
    {

        UIPermission uiPerm1 = new UIPermission(UIPermissionWindow.SafeTopLevelWindows);
        UIPermission uiPerm2 = new UIPermission(PermissionState.None);
        uiPerm2 = (UIPermission)uiPerm1.Copy();
        if (uiPerm2 != null)
        {
            Console.WriteLine("The copy succeeded:  " + uiPerm2.ToString() + " \n");
        }

    }
    // ToXml creates an XML encoding of the permission and its current state; FromXml reconstructs a
    // permission with the specified state from the XML encoding.
    private static void ToFromXmlDemo()
    {


        UIPermission uiPerm1 = new UIPermission(UIPermissionWindow.SafeTopLevelWindows);
        UIPermission uiPerm2 = new UIPermission(PermissionState.None);
        uiPerm2.FromXml(uiPerm1.ToXml());
        bool result = uiPerm2.Equals(uiPerm1);
        if (result)
        {
            Console.WriteLine("Result of ToFromXml = " + uiPerm2.ToString());
        }
        else
        {
            Console.WriteLine(uiPerm2.ToString());
            Console.WriteLine(uiPerm1.ToString());
        }

    }

}


System.Object
   System.Security.CodeAccessPermission
    System.Security.Permissions.UIPermission

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft