Example

using System;
using System.Security;
using System.Security.Permissions;

namespace Samples
{

  public class SecurityExceptionSample
  {
    [EnvironmentPermissionAttribute(
                SecurityAction.PermitOnly,
                Read="USERNAME")]
    static void Main(string[] args)
    {
      string s = "COMPUTERNAME";
      try
      {
        Console.WriteLine("Variable {0} Value: {1}", s,
                Environment.GetEnvironmentVariable(s));
      }
      catch(SecurityException e)
      {
        Console.WriteLine("Exception {0}", e);
      }
    }
  }
}
The output is:
Exception System.Security.SecurityException: Request for the permission of type System
.Security.Permissions.EnvironmentPermission, mscorlib, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 ...

Get .NET Framework Standard Library Annotated Reference, Volume 1: Base Class Library and Extended Numerics Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.