首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

为什么要请求权限?该如何处理

2012-03-17 
为什么要请求权限?我对代码权限不是很理解。有时根本不用请求权限也可以正常运行代码,为什么还要请求权限呢

为什么要请求权限?
我对代码权限不是很理解。有时根本不用请求权限也可以正常运行代码,为什么还要请求权限呢?

using   System.Security.Permissions;

//在程序集范围内请求最小权限
[assembly:   FileIOPermission(SecurityAction.RequestMinimum,   Unrestricted   =   true)]

namespace   MyNameSpace
{
        using   System;
        using   System.Security;
        using   System.Security.Permissions;
        using   System.IO;

        public   class   MyClass
        {
                public   MyClass()
                {
                }

                public   static   int   Main(string[]   args)
                {
                        //Creation   of   the   log   is   attempted   in   the   try   block.
                        try
                        {
                                StreamWriter   TextStream   =   new   StreamWriter( "Log.txt ");
                                TextStream.WriteLine( "This   Log   was   created   on   {0} ",   DateTime.Now);
                                TextStream.Close();
                                Console.WriteLine( "The   Log   was   created ");
                        }
                        //Catch   the   Security   exception   and   inform   the   user   that   the  
                        //application   was   not   granted   FileIOPermission.
                        catch   (SecurityException)
                        {
                                Console.WriteLine( "This   application   does   not   have   permission   to   write   to   the  

disk. ");
                        }


                        return   0;
                }
        }
}
以上代码不请求权限也可以,为什么还要请求权限呢?

[解决办法]
因为有对本地资源的操作.
------解决方案--------------------


CAS里面的问题都不是简单的问题,尤其是微软对这一块的文档总是含含糊糊。

我的理解是,请求权限实际上是说,所要求的权限,如果调用方低于这个权限,那么调用将不会成功。

热点排行