This class handles opening a file and returning the contents.

 

using System;
using System.Security.Permissions;
using System.Security;
using System.IO;
[assembly: FileIOPermission(SecurityAction.RequestMinimum, Read = @"c:\temp.txt")]
namespace FileIOClass
{

 public class SecureIO
 {
  private string fname = @"c:\temp.txt";
  public SecureIO()
  {

  }

  public string OpenFile()
  {
   StreamReader sr = new StreamReader(fname);
   return sr.ReadToEnd();
  }
  public string OpenFileWithAssert()
  {
   FileIOPermission filePerm = new FileIOPermission(
    FileIOPermissionAccess.AllAccess, fname);
   filePerm.Assert();
   StreamReader sr = new StreamReader(fname);
   string s = sr.ReadToEnd();
   CodeAccessPermission.RevertAssert();
   return s;
  }
  public string OpenFileWithDemandAndAssert()
  {
   RegistryPermission regPerm = new RegistryPermission(PermissionState.Unrestricted);
   regPerm.Demand();
   FileIOPermission filePerm = new FileIOPermission(
    FileIOPermissionAccess.AllAccess, fname);
   filePerm.Assert();
   StreamReader sr = new StreamReader(fname);
   string s = sr.ReadToEnd();
   CodeAccessPermission.RevertAssert();
   return s;
  }
 }
}