Hello!
I have a simple application from a book where the actual applications
permission doesn't match the permission referenced from the book.
My question is written further down.
The book is saying the following : "The calculate permission button from the
security properties of Visual Studio analysis is
an application manifest that includes all required permissions. With Visual
Studio 2005, you can see the
application manifest with the name app.manifest below Properties in the
Solution Explorer. The content of this file is shown
here. The XML element <applicationReq uestMinimumdefi nes all required
permissions of the application.
The FileIOPermissio n is required because the application reads and writes
files using classes from System.IO namaspace."
Below is a copy of app.manifest.
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assemb ly manifestVersion ="1.0"
xmlns="urn:sche mas-microsoft-com:asm.v1"
xmlns:asmv1="ur n:schemas-microsoft-com:asm.v1"
xmlns:asmv2="ur n:schemas-microsoft-com:asm.v2"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance">
<trustInfo xmlns="urn:sche mas-microsoft-com:asm.v2">
<security>
<applicationReq uestMinimum>
<defaultAssembl yRequest permissionSetRe ference="Custom " />
<PermissionSe t class="System.S ecurity.Permiss ionSet" version="1"
ID="Custom" SameSite="site" >
<IPermission
class="System.S ecurity.Permiss ions.Reflection Permission, mscorlib,
Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9"
version="1" Unrestricted="t rue" />
<IPermission
class="System.S ecurity.Permiss ions.SecurityPe rmission, mscorlib,
Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9"
version="1" Flags="Unmanage dCode, Execution, ControlEvidence " />
<IPermission class="System.S ecurity.Permiss ions.UIPermissi on,
mscorlib, Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9"
version="1" Unrestricted="t rue" />
<IPermission
class="System.S ecurity.Permiss ions.KeyContain erPermission, mscorlib,
Version=2.0.0.0 , Culture=neutral , PublicKeyToken= b77a5c561934e08 9"
version="1" Unrestricted="t rue" />
</PermissionSet>
</applicationRequ estMinimum>
</security>
</trustInfo>
</asmv1:assembly>
Below is the main class and as you can see I use file IO operation so the
application needs the FileIOPermissio n.
Now to my question: When I click on the "calculate Permission" button in the
sequrity after choosing the property for the
project the FileIOPermissio n is not selected as a requirement for the
application but according to the book the appliaction
needs this requirement and I must agree with the book in this case.
Can anyone explain the reason why the sequrity button named calculate
permission doesn't include FileIOPermissio n for
the application as a permission requirement ?
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows. Forms;
using System.Drawing. Printing;
namespace SimpleEditor
{
public partial class SimpleEditorFor m : Form
{
private string filename = "Untitled";
private string[] lines;
private int linesPrinted;
private Brush printBrush;
public SimpleEditorFor m() //User defined C-tor
{ InitializeCompo nent(); }
protected void OpenFile()
{
try
{
textBoxEdit.Cle ar();
textBoxEdit.Tex t = File.ReadAllTex t(filename);
}
catch(IOExcepti on ex)
{
MessageBox.Show (ex.Message, "Simple Editor",
MessageBoxButto ns.OK, MessageBoxIcon. Exclamation);
}
}
private void OnFileNew(objec t sender, EventArgs e)
{
filename = "Untitled";
SetFormTitle();
textBoxEdit.Cle ar(); // clear textbox
}
private void OnFileOpen(obje ct sender, EventArgs e)
{
if (dlgOpenFile.Sh owDialog() == DialogResult.OK )
{
filename = dlgOpenFile.Fil eName;
SetFormTitle();
OpenFile();
}
}
private void OnFileSave(obje ct sender, EventArgs e)
{
if (filename == "Untitled")
OnFileSaveAs(se nder, e);
else
SaveFile();
}
private void OnFileSaveAs(ob ject sender, EventArgs e)
{
if (dlgSaveFile.Sh owDialog() == DialogResult.OK )
{
filename = dlgSaveFile.Fil eName;
SetFormTitle();
SaveFile();
}
}
private void SaveFile()
{
try
{
File.WriteAllTe xt(filename, this.textBoxEdi t.Text);
}
catch(IOExcepti on ex)
{
MessageBox.Show (ex.Message, "Simple Editor",
MessageBoxButto ns.AbortRetryIg nore,MessageBox Icon.Hand);
}
}
protected void SetFormTitle()
{
Text = new FileInfo(filena me).Name + "- Simple Editor";
}
private void OnFilePrint(obj ect sender, EventArgs e)
{
if (this.textBoxEd it.SelectedText != "")
dlgPrint.AllowS election = true;
if (dlgPrint.ShowD ialog() == DialogResult.OK )
printDocument.P rint();
}
private void OnFilePrintPrev iew(object sender, EventArgs e)
{ dlgPrintPreview .ShowDialog(); }
private void OnFilePageSetup (object sender, EventArgs e)
{ dlgPageSetup.Sh owDialog(); }
private void OnExit(object sender, EventArgs e)
{ Application.Exi t(); }
private void OnPrintPage(obj ect sender, PrintPageEventA rgs e)
{
int x = e.MarginBounds. Left;
int y = e.MarginBounds. Top;
while (linesPrinted < lines.Length) //antal rader att printa
{
e.Graphics.Draw String(lines[linesPrinted++],
this.fontDialog .Font, printBrush, x, y);
y += textBoxEdit.Fon t.Height;
if (y >= e.MarginBounds. Bottom)
{
e.HasMorePages = true;
return;
}
}
}
private void OnBeginPrint(ob ject sender, PrintEventArgs e)
{
char[] param = { '\n' };
if (dlgPrint.Print erSettings.Prin tRange == PrintRange.Sele ction)
lines = textBoxEdit.Sel ectedText.Split (param);
else
lines = textBoxEdit.Tex t.Split(param);
//int i = 0;
//char[] trimParam = { '\r' };
//foreach (string s in lines)
// lines[i++] = s.TrimEnd(trimP aram);
if (this.dlgPrint. PrinterSettings .SupportsColor)
printBrush = new SolidBrush(text BoxEdit.ForeCol or);
else
printBrush = Brushes.Black;
}
private void OnEndPrint(obje ct sender, PrintEventArgs e)
{ lines = null; }
private void fontToolStripMe nuItem_Click(ob ject sender, EventArgs e)
{
if (fontDialog.Sho wDialog() == DialogResult.OK )
textBoxEdit.Fon t = fontDialog.Font ;
}
private void colorToolStripM enuItem_Click(o bject sender, EventArgs
e)
{
if (colorDialog.Sh owDialog() == DialogResult.OK )
textBoxEdit.For eColor = colorDialog.Col or;
}
}
}
//Tony