Search code examples
c#.netcasexcel-dna

SecurityException occured, Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib'


On this line I get the following exception:

using (Stream reader = File.Open(m_inputFile, FileMode.Open))

Exception:

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

This happens in an Excel addin running in ExcelDNA, however this fails as soon as the XLL for Excel is put on a network drive.

How can I allow full permissions to read from local drives as I am on a secure network ?


Solution

  • This is probably due to the default security settings under .NET 2.0, which grant restricted permissions to code running over a network.

    The first step is the ensure that you are running a recent version of Excel-DNA - there were some changes related to the security of the AppDomain setup a few years ago that might be relevant.

    Another fix you can try is to retarget your add-in to .NET 4.0. You probably don't even have to recompile, just change your .dna file to start with:

    <DnaLibrary RuntimeVersion="v4.0" Name="..." >
    

    If you want to stick with the .NET 2.0 runtime, you can change the permissions by using the caspol utility. There are some detailed instructions in this post on the Google group: https://groups.google.com/d/msg/exceldna/Csls-_bjDp0/G3HlxFkDefQJ