Tag Archives: cas

FileIOPermission

To check for file permission you still have to try to openWrite it .
just check this out you still have to open and test it cause demand
only checks for CAS.. and well the code does have access to the
concept of the directory and the file .. but its NTFS that inhibits
this access.

using System;using System.IO;public class FileTest{public static void Main(){  string fileName = @"c:Securitytext.txt";  System.Console.Write("test");    try    {      Console.WriteLine("File exists: " +      System.IO.File.Exists(fileName).ToString());      FileStream fs;      System.Security.Permissions.FileIOPermission f = new      System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.AllAccess,      fileName);      f.Demand(); //Somehow i feel that this should have been enough..       Console.WriteLine("Demand checked");      if ((fs = System.IO.File.OpenRead(fileName)) != null)      {        fs.Close();        Console.WriteLine("File Closed");      }    }    catch (System.IO.IOException ex)    {    // Console.WriteLine(ex.StackTrace);     Console.WriteLine("Message " + ex.Message);    }    catch (Exception ex)    {    Console.WriteLine(ex.StackTrace);   }  }}