This project has moved. For the latest updates, please go here.

System.Security.Permissions.IsolatedStorageFilePermission

Feb 4, 2013 at 9:21 PM
Edited Feb 4, 2013 at 9:24 PM
I'm getting a security error when creating an excel file:

__Security Exception

Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.IsolatedStorageFilePermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.__

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[SecurityException: Request for the permission of type 'System.Security.Permissions.IsolatedStorageFilePermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark) +31
System.Security.CodeAccessPermission.Demand() +46
System.IO.IsolatedStorage.IsolatedStorage.DemandPermission(IsolatedStorageScope scope) +769
System.IO.IsolatedStorage.IsolatedStorage.InitStore(IsolatedStorageScope scope, Evidence domainEv, Type domainEvidenceType, Evidence assemEv, Type assemblyEvidenceType, Evidence appEv, Type appEvidenceType) +51
System.IO.IsolatedStorage.IsolatedStorage.InitStore(IsolatedStorageScope scope, Type domainEvidenceType, Type assemblyEvidenceType) +160
System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(IsolatedStorageScope scope, Type domainEvidenceType, Type assemblyEvidenceType) +59
MS.Internal.IO.Packaging.ReliableIsolatedStorageFileFolder.GetCurrentStore() +48
MS.Internal.IO.Packaging.ReliableIsolatedStorageFileFolder..ctor() +64
MS.Internal.IO.Packaging.PackagingUtilities.GetDefaultIsolatedStorageFile() +54
MS.Internal.IO.Packaging.PackagingUtilities.CreateUserScopedIsolatedStorageFileStreamWithRandomName(Int32 retryCount, String& fileName) +122
MS.Internal.IO.Packaging.SparseMemoryStream.SwitchModeIfNecessary() +373
MS.Internal.IO.Packaging.SparseMemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) +189
MS.Internal.IO.Packaging.CompressEmulationStream.Write(Byte[] buffer, Int32 offset, Int32 count) +60
MS.Internal.IO.Packaging.CompressStream.Write(Byte[] buffer, Int32 offset, Int32 count) +138
MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.Write(Byte[] buffer, Int32 offset, Int32 count) +170
MS.Internal.IO.Zip.ZipIOModeEnforcingStream.Write(Byte[] buffer, Int32 offset, Int32 count) +86
System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder) +98
System.IO.StreamWriter.Write(String value) +109
System.IO.TextWriter.Write(String format, Object arg0, Object arg1) +90
OfficeOpenXml.ExcelWorksheet.UpdateRowCellData(StreamWriter sw) +5750
OfficeOpenXml.ExcelWorksheet.SaveXml() +537
OfficeOpenXml.ExcelWorksheet.Save() +218
OfficeOpenXml.ExcelWorkbook.Save() +476
OfficeOpenXml.ExcelPackage.Save() +52


Here's my code:
    public static FileInfo GenerateExcelFile(string filePath, DataTable dt, string headerText)
    {
        using (ExcelPackage pck = new ExcelPackage())
        {
            //Create the worksheet
            ExcelWorksheet ws = pck.Workbook.Worksheets.Add(string.IsNullOrEmpty(dt.TableName) ? "Sheet1" : dt.TableName);
            int startRow = 1;

            if (!string.IsNullOrEmpty(headerText))
            {
                ws.Cells[1, 1].Value = headerText;
                startRow = 2;
            }

            ws.Cells[startRow, 1].LoadFromDataTable(dt, true);

            pck.SaveAs(new FileInfo(filePath));


        }

        return new FileInfo(filePath);
    }