FileDialogPermission.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / clr / src / BCL / System / Security / Permissions / FileDialogPermission.cs / 1 / FileDialogPermission.cs

                            // ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
// FileDialogPermission.cs
// 
 
namespace System.Security.Permissions {
    using System; 
    using System.Text;
    using System.Security;
    using System.Security.Util;
    using System.IO; 
    using System.Runtime.Serialization;
    using System.Reflection; 
    using System.Collections; 
    using System.Globalization;
 
    [Serializable, Flags]
[System.Runtime.InteropServices.ComVisible(true)]
    public enum FileDialogPermissionAccess {
        None = 0x00, 

        Open = 0x01, 
 
        Save = 0x02,
 
        OpenSave = Open | Save

    }
 
    [Serializable]
[System.Runtime.InteropServices.ComVisible(true)] 
    public sealed class FileDialogPermission : CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission { 
        FileDialogPermissionAccess access;
 
        public FileDialogPermission(PermissionState state) {
            if (state == PermissionState.Unrestricted) {
                SetUnrestricted(true);
            } 
            else if (state == PermissionState.None) {
                SetUnrestricted(false); 
                Reset(); 
            }
            else { 
                throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
            }
        }
 
        public FileDialogPermission(FileDialogPermissionAccess access) {
            VerifyAccess(access); 
            this.access = access; 
        }
 
        public FileDialogPermissionAccess Access {
            get {
                return access;
            } 

            set { 
                VerifyAccess(value); 
                access = value;
            } 
        }

        public override IPermission Copy() {
            return new FileDialogPermission(this.access); 
        }
 
        public override void FromXml(SecurityElement esd) { 
            CodeAccessPermission.ValidateElement(esd, this);
            if (XMLUtil.IsUnrestricted(esd)) { 
                SetUnrestricted(true);
                return;
            }
 
            access = FileDialogPermissionAccess.None;
 
            string accessXml = esd.Attribute("Access"); 
            if (accessXml != null)
                access = (FileDialogPermissionAccess)Enum.Parse(typeof(FileDialogPermissionAccess), accessXml); 
        }

        /// 
        int IBuiltInPermission.GetTokenIndex() { 
            return FileDialogPermission.GetTokenIndex();
        } 
 
        internal static int GetTokenIndex() {
            return BuiltInPermissionIndex.FileDialogPermissionIndex; 
        }

        public override IPermission Intersect(IPermission target) {
            if (target == null) { 
                return null;
            } 
            else if (!VerifyType(target)) { 
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongType"), this.GetType().FullName));
            } 

            FileDialogPermission operand = (FileDialogPermission)target;

            FileDialogPermissionAccess intersectAccess = access & operand.Access; 

            if (intersectAccess == FileDialogPermissionAccess.None) 
                return null; 
            else
                return new FileDialogPermission(intersectAccess); 
        }

        public override bool IsSubsetOf(IPermission target) {
            if (target == null) { 
                // Only safe subset if this is empty
                return access == FileDialogPermissionAccess.None; 
            } 

            try { 
                FileDialogPermission operand = (FileDialogPermission)target;
                if (operand.IsUnrestricted()) {
                    return true;
                } 
                else if (this.IsUnrestricted()) {
                    return false; 
                } 
                else {
                    int open = (int)(access & FileDialogPermissionAccess.Open); 
                    int save = (int)(access & FileDialogPermissionAccess.Save);
                    int openTarget = (int)(operand.Access & FileDialogPermissionAccess.Open);
                    int saveTarget = (int)(operand.Access & FileDialogPermissionAccess.Save);
 
                    return open <= openTarget && save <= saveTarget;
                } 
            } 
            catch (InvalidCastException) {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongType"), this.GetType().FullName)); 
            }

        }
 
        public bool IsUnrestricted() {
            return access == FileDialogPermissionAccess.OpenSave; 
        } 

        void Reset() { 
            access = FileDialogPermissionAccess.None;
        }

        void SetUnrestricted( bool unrestricted ) { 
            if (unrestricted) {
                access = FileDialogPermissionAccess.OpenSave; 
            } 
        }
 
        public override SecurityElement ToXml() {
            SecurityElement esd = CodeAccessPermission.CreatePermissionElement( this, "System.Security.Permissions.FileDialogPermission" );
            if (!IsUnrestricted()) {
                if (access != FileDialogPermissionAccess.None) { 
                    esd.AddAttribute("Access", Enum.GetName(typeof(FileDialogPermissionAccess), access));
                } 
            } 
            else {
                esd.AddAttribute("Unrestricted", "true"); 
            }
            return esd;
        }
 
        public override IPermission Union(IPermission target) {
            if (target == null) { 
                return this.Copy(); 
            }
            else if (!VerifyType(target)) { 
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_WrongType"), this.GetType().FullName));
            }

            FileDialogPermission operand = (FileDialogPermission)target; 
            return new FileDialogPermission(access | operand.Access);
        } 
 
        static void VerifyAccess(FileDialogPermissionAccess access) {
            if ((access & ~FileDialogPermissionAccess.OpenSave) != 0 ) { 
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Arg_EnumIllegalVal"), (int)access));
            }
        }
    } 
}


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK