Easy Question Re: FileIOPermission

Posted on 2008-11-10
Medium Priority
Last Modified: 2013-12-17
I am writing a very simple C# application to help with my kids' reading.  The app consists of a form with a button that brings up randomly one of a hundred words stored in an array and the play button plays me reading that word stored in an individual .wav file.  The program plays fine on my computer but when I run it on their computer I get the following error message:

System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
   at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
   at System.Security.CodeAccessPermission.Demand()
   at System.IO.Directory.GetCurrentDirectory()
   at TopWords.Form1.button2_Click_1(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
The action that failed was:
The type of the first permission that failed was:
The Zone of the assembly that failed was:

My first version of the code did not have the Directory.GetCurrentDirectory() method and looked for the .wav files in a set place.  I modified it this morning and now get the message as above.  Here is the code from the button click method:  
private void button2_Click_1(object sender, EventArgs e)
        if (label1.Text != "          ")
            FileIOPermission f = new FileIOPermission(PermissionState.None);
            f.AllLocalFiles = FileIOPermissionAccess.AllAccess;
            string currentDirectory = Directory.GetCurrentDirectory();
            System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer();
            string FileName;
            FileName = currentDirectory +"\\"+label1.Text+".wav";
            myPlayer.SoundLocation = @FileName;

Any help would be appreciated...I am very novice with C# and .Net.
Question by:djma
  • 2
  • 2
LVL 96

Expert Comment

by:Bob Learned
ID: 22930839

It sounds like you are running the application on one machine from a folder on your development machine.  There are zones in the .NET security framework.  My Computer gets full trust by default, but anything else is considered a partially trusted environment.

The term is "Code Access Security", and has a pretty large footprint.

Understanding .NET Code Access Security

Author Comment

ID: 22934184
No, I am running as a standalone application from a desktop machine loading the file from my server.  It is funny, the application (this is my first c# app) took me half an hour to write and this error has taken me hours to track down.  .NET is extremely complicated for a middle aged newbie.

I am looking through the Code Access Security link which co-incidentally I came across when doing my first pass through for a solution.  Although I will try and code this myself if you could tell my which namespace I need and how to call for FullTrust...it would be appreciated.
LVL 96

Expert Comment

by:Bob Learned
ID: 22934280
One way to solve this is to make sure that the computer has My Computer zone set to Full Trust.

Start Menu | Administrative Tools | Microsoft .NET Framework 2.0 Configuration | Configure Code Access Security Policy | Adjust Zone Security

Make changes to this computer
My Computer is selected by default
Slider needs to be at the top (Full Trust)
LVL 41

Accepted Solution

graye earned 500 total points
ID: 22942351
Be aware that you kid's PC might not have the control panel applet installed.
Take a look at the following article that provides a technique installing the missing applet (and for adjusting CAS without the control panel appet):  http://home.hot.rr.com/graye/Articles/CodeAccessSecurity.htm

Author Closing Comment

ID: 31516144
Thanks everyone for your help.  I was able to fix it by adjusting the Intranet zone in Internet Explorer and adding my server drive to the list.  Nevertheless, this seems extremely complicated for an old fart like me who only wanted to write a quick app for his kids.  Anyway, I guess I should be using a less sophisticated (and complicated) tool.  I could have done the whole thing in a few minutes using FoxPro but I thought it would be fun to use c#.  Anyway, I am now wiser.  Thanks again.

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question