Check for full trust

1.1k views Asked by At

How do I check if my code is running in full trust mode? I have seen several suggestions to check for specific permissions using SecurityManager.IsGranted() method, but I specifically want to check for full trust because no specific permission in partial trust mode is enough to use FileSystemWatcher class.

1

There are 1 answers

0
Nicole Calinoiu On

AFAIK, there's only one approach that will work for transparent code (which yours will presumably be under at least 4.0 if it's not fully trusted) in both 3.5 and 4.0: demanding an unrestricted permission set and catching the SecurityException if the demand fails. e.g.:

public static bool RunningWithFullTrust()
{
    bool result;
    try
    {
        (new PermissionSet(PermissionState.Unrestricted)).Demand();
        result = true;
    }
    catch (SecurityException)
    {
        result = false;
    }

    return result;
}

This probably offers no advantage whatsoever over your current approach of catching and ignoring the exception.

For 4.0, there is a new AppDomain.IsFullyTrusted method that would potentially be of use.