2011-01-19 85 views

回答

0

從dmitryr的博客post

public static AspNetHostingPermissionLevel GetTrustLevel() 
{ 
    foreach (AspNetHostingPermissionLevel trustLevel in new AspNetHostingPermissionLevel[] 
                  { 
                   AspNetHostingPermissionLevel.Unrestricted, 
                   AspNetHostingPermissionLevel.High, 
                   AspNetHostingPermissionLevel.Medium, 
                   AspNetHostingPermissionLevel.Low, 
                   AspNetHostingPermissionLevel.Minimal 
                  }) 
    { 
     try 
     { 
      new AspNetHostingPermission(trustLevel).Demand(); 
     } 
     catch (System.Security.SecurityException) 
     { 
      continue; 
     } 

     return trustLevel; 
    } 

    return AspNetHostingPermissionLevel.None; 
} 
0

或者您可以使用此略短版

public AspNetHostingPermissionLevel GetCurrentTrustLevel() 
{ 
    foreach (AspNetHostingPermissionLevel trustLevel in Enum.GetValues(typeof(AspNetHostingPermissionLevel))) 
    { 
     try 
     { 
      new AspNetHostingPermission(trustLevel).Demand(); 
     } 
     catch (System.Security.SecurityException) 
     { 
      continue; 
     } 
     return trustLevel; 
    } 
    return AspNetHostingPermissionLevel.None; 
}