Giving File IO permissions dynamically

By using the FileIOPermission the user can be give access to read the file or not . By using the following code the user can achieve this .


string filename = @"d:\Test\id.txt";
System.Security.Permissions.FileIOPermission permission = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.Read ,filename);

try
{
permission.Deny();
//permission.Demand (); // If certain point of time if the user wants to access the // permission then he can use this code .


System.IO.StreamReader sr = new System.IO.StreamReader(filename);
Response.Write(sr.ReadToEnd());

}
catch (System.Security.SecurityException ex)
{
Response.Write("Permission to read the file was denied " );
}
catch (System.Exception ex)
{
Response.Write(ex.Message);
}


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: