0% found this document useful (0 votes)
30 views

Netframework

This document demonstrates using .NET Framework code access security to give code permissions independent of the executing user. It contains C# source code for a Windows form with two buttons - one creates a file without error handling and one with error handling. When clicked, they attempt to create files in the C:\ directory and output status messages to indicate if they succeeded or failed. This allows showing how code access security works when code attempts actions the user may not have permission for.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Netframework

This document demonstrates using .NET Framework code access security to give code permissions independent of the executing user. It contains C# source code for a Windows form with two buttons - one creates a file without error handling and one with error handling. When clicked, they attempt to create files in the C:\ directory and output status messages to indicate if they succeeded or failed. This allows showing how code access security works when code attempts actions the user may not have permission for.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Aim:- Demonstrate the use of >NET Framework Code Access Security, in which

code can have permissions independent of the person executing the code

Source code:-
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace WindowsApplication27
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button
CreateFileNoErrorHandling;
private System.Windows.Forms.Button
CreateFileWithErrorHandling;
private System.Windows.Forms.StatusBar statusBar1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.CreateFileNoErrorHandling = new
System.Windows.Forms.Button();
this.CreateFileWithErrorHandling = new
System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.SuspendLayout();
this.CreateFileNoErrorHandling.BackColor =
System.Drawing.Color.FromArgb(((int)(((byte)(255)))),
((int)(((byte)(224)))), ((int)(((byte)(192)))));
this.CreateFileNoErrorHandling.ForeColor =
System.Drawing.Color.FromArgb(((int)(((byte)(0)))),
((int)(((byte)(0)))), ((int)(((byte)(192)))));
this.CreateFileNoErrorHandling.Location = new
System.Drawing.Point(128, 48);
this.CreateFileNoErrorHandling.Name =
"CreateFileNoErrorHandling";
this.CreateFileNoErrorHandling.Size = new
System.Drawing.Size(112, 23);
this.CreateFileNoErrorHandling.TabIndex = 0;
this.CreateFileNoErrorHandling.Text = "No Error Handling";
this.CreateFileNoErrorHandling.UseVisualStyleBackColor =
false;
this.CreateFileNoErrorHandling.Click += new
System.EventHandler(this.CreateFileNoErrorHandling_Click);
this.CreateFileWithErrorHandling.BackColor =
System.Drawing.Color.FromArgb(((int)(((byte)(255)))),
((int)(((byte)(224)))), ((int)(((byte)(192)))));
this.CreateFileWithErrorHandling.ForeColor =
System.Drawing.Color.FromArgb(((int)(((byte)(0)))),
((int)(((byte)(0)))), ((int)(((byte)(192)))));
this.CreateFileWithErrorHandling.Location = new
System.Drawing.Point(304, 48);
this.CreateFileWithErrorHandling.Name =
"CreateFileWithErrorHandling";
this.CreateFileWithErrorHandling.Size = new
System.Drawing.Size(112, 23);
this.CreateFileWithErrorHandling.TabIndex = 1;
this.CreateFileWithErrorHandling.Text = "With Error
Handling";
this.CreateFileWithErrorHandling.UseVisualStyleBackColor =
false;
this.CreateFileWithErrorHandling.Click += new
System.EventHandler(this.CreateFileWithErrorHandling_Click);
this.statusBar1.Location = new System.Drawing.Point(0, 425);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(608, 22);
this.statusBar1.TabIndex = 2;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(608, 447);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.CreateFileWithErrorHandling);
this.Controls.Add(this.CreateFileNoErrorHandling);
this.Name = "Form1";
this.Text = "Code Access Security";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

static void Main()


{
Application.Run(new Form1());
}

private void CreateFileNoErrorHandling_Click(object sender,


System.EventArgs e)
{ System.IO.File.Create("c:\\test1.txt");
statusBar1.Text = "File (test1.txt) Created
Successfully";

}
private void CreateFileWithErrorHandling_Click(object
sender, System.EventArgs e)
{
try
{
System.IO.File.Create("c:\\test2.txt");
statusBar1.Text = "File (test2.txt) Created
Successfully";

}
catch (Exception ex)
{
statusBar1.Text = "The following error occurred
" + ex.Message + " creating file (test2.txt)";
}
}

private void Form1_Load(object sender, EventArgs e)


{

}
}
}

Output:-

Result:- The programme is executed successfully without any errors.

You might also like