前面的话确实是从msdn抄出来的

解决方案 »

  1.   

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconrequestingoptionalpermissions.asp
      

  2.   

    Unrestricted属性基本明白了。那么
    [assembly:FileIOPermission(SecurityAction.RequestMinimum)]
    [assembly:FileIOPermission(SecurityAction.RequestRefuse )]
    [assembly:FileIOPermission(SecurityAction.RequestOptional, Unrestricted = true)]
    这三个运行效果为什么是一样的呢?代码如下:using System.Security.Permissions;
    using System.IO;
    using System;[assembly:FileIOPermission(SecurityAction.RequestOptional, Unrestricted = true)]namespace MyNamespace 
    {
    using System;
    using System.Security;
    //The hypothetical class log is in this namespace.
    public class MyClass 
    {
    public MyClass() 
    {
    } public static void Main(string[] args) 
    {
    //Put any code that requires optional permissions in the try block. 
    try 
    {
    FileStream fs = new FileStream(@"d:\mcWindowsService.txt" , FileMode.OpenOrCreate, FileAccess.Write); 
    StreamWriter m_streamWriter = new StreamWriter(fs); 
    m_streamWriter.BaseStream.Seek(0, SeekOrigin.End); 
    m_streamWriter.WriteLine("The Log has been created.");
    m_streamWriter.Flush(); 
    Console.WriteLine("The Log has been 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.");
    }
    Console.ReadLine();
    }
    }
    }
      

  3.   

    Msdn对RequestMinimum 、RequestOptional 、RequestRefuse的区别的解释是:The SecurityAction.RequestOptional flag allows you to request a set of permissions while refusing all other permissions the runtime otherwise might have been willing to give. By contrast, the RequestRefuse flag allows you to refuse permissions by explicitly specifying which ones your code should not be granted.  In contrast to using the RequestMinimum flag, your application will execute if it does not receive all the permissions that you request using the RequestOptional flag, and a SecurityException will be thrown when your application attempts to access a protected resource. If you use this type of request, you must enable your code to catch any exceptions that will be thrown if your code is not granted the optional permission.我的翻译希望没有错:RequestOptional允许你在运行时获得指定的权限,同时拒绝获得其他权限。而RequestRefuse 则明确地告诉你,这些权限你不能获得。如果使用RequestMinimum,你的程序会在你没有完全获得RequestOptional所要求全部权限的时候运行,当你的程序试图访问一个保护的资源时就会抛出一个SecurityException的异常。所以如果你定义了optional的permission,那么请加入catch代码。
      

  4.   

    the last words,i cann't agree with your view
    它是说:如果用RequestMinimum的话,最好用try===catch结构,这样的话呢就能够发现,如果你试图访问被保护资源
      

  5.   

    [assembly:FileIOPermission(SecurityAction.RequestMinimum)]
    [assembly:FileIOPermission(SecurityAction.RequestRefuse )]
    [assembly:FileIOPermission(SecurityAction.RequestOptional, Unrestricted = true)]
    这三个运行效果是一样的,我觉得好像是因为我执行的程序的时候,已经有了某些权限了。 khpcg(欢乐英雄) 我想问问,是不是权限是根据windows登入的账户决定的,还是可以根据程序运行情况,动态地分配?