tags:

views:

75

answers:

2

I am using OpenFileDialog class to browse a file in window application using c#. It is giving the security exception as below.

Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

My code is

        OpenFileDialog fdlg = new OpenFileDialog();
        fdlg.Title = "C# Corner Open File Dialog";
        fdlg.InitialDirectory = @"c:\";
        fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
        fdlg.FilterIndex = 2;
        fdlg.RestoreDirectory = true;
        if (fdlg.ShowDialog() == DialogResult.OK)
        {
            txtpath.Text = fdlg.FileName;
        }

I am not getting the problem. please help.

A: 

Security Note
To get or set the FileName property, your assembly requires a privilege level granted by the System.Security.Permissions.FileIOPermission class. If you are running in a partial-trust context, the process might throw an exception due to insufficient privileges. For more information, see Code Access Security Basics.

Joe Garrett
You should add a source (and perhaps a link) for your quote. And also perhaps explain what it actually means for those that don't understand...
Dan Puzey
A: 

If you are actually trying to run this code from an ASP.NET application, then the exception you receive makes sense. This is Windows Forms code, and is not meant to run in an ASP.NET application.

Among other things, ASP.NET applications run in a reduced-trust environment. They are not allowed to do certain things that a "normal" application can do.


.NET includes a concept called "Code Access Security". It grants different access to different .NET features depending on things like where the code comes from. Naturally, code running from your computer is more trusted that code running from some other computer. In your environment, that means it's not trusted to access the file system in this manner.

This code will need to be copied to a local drive and run from there.

John Saunders
I am running this code on .NET window /desktop application
Ankita_K
Then why did you tag the question asp.net?
John Saunders
sorry by mistake
Ankita_K
can u tell me what is the problem ?
Ankita_K
yes I am trying to run it on shared drive
Ankita_K
shared drive ?? u must hv full permission on shared drive. to access/read the file..