日期:2014-05-19  浏览次数:20682 次

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

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;
                }
        }
}
以上代码不请求权限也可以,为什么还要请求权限呢?

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