Easy Question Re: FileIOPermission

Posted on 2008-11-10
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
    LVL 96

    Expert Comment

    by:Bob Learned

    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

    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 would be appreciated.
    LVL 96

    Expert Comment

    by:Bob Learned
    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

    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):

    Author Closing Comment

    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

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now