Monday, April 14, 2008

Code Access Security - Code Snippets

Refuse writing to a drive

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

[assembly: FileIOPermissionAttribute(SecurityAction.RequestRefuse , Write="d:\\")]
[assembly:FileIOPermissionAttribute(SecurityAction.RequestMinimum, Read=@"c:\")]
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

StreamWriter sw;
private void button1_Click(object sender, EventArgs e)
{
sw = File.CreateText("d:\\Shalvin.txt");
sw.WriteLine("Hello");
sw.Close();
}
}
}

You will receive a runtime error when you attempt to write to d:.

No comments:

Post a Comment