Sep 16 2009
Access is Denied Exception when writing to Spreadsheet from ASP.Net
I recently got the following error message whilst trying to write to an excel file using the openXML library in ASP.Net and C#:
System.ApplicationException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) at System.IO.IsolatedStorage.IsolatedStorageFile.nGetRootDir(IsolatedStorageScope scope) at System.IO.IsolatedStorage.IsolatedStorageFile.InitGlobalsNonRoamingUser(IsolatedStorageScope scope) at System.IO.IsolatedStorage.IsolatedStorageFile.GetRootDir(IsolatedStorageScope scope) at System.IO.IsolatedStorage.IsolatedStorageFile.GetGlobalFileIOPerm(IsolatedStorageScope scope) at System.IO.IsolatedStorage.IsolatedStorageFile.Init(IsolatedStorageScope scope) at System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(IsolatedStorageScope scope, Type domainEvidenceType, Type assemblyEvidenceType) at MS.Internal.IO.Packaging.PackagingUtilities.ReliableIsolatedStorageFileFolder..ctor() at MS.Internal.IO.Packaging.PackagingUtilities.GetDefaultIsolatedStorageFile() at MS.Internal.IO.Packaging.PackagingUtilities.CreateUserScopedIsolatedStorageFileStreamWithRandomName(Int32 retryCount, String& fileName) at MS.Internal.IO.Packaging.SparseMemoryStream.EnsureIsolatedStoreStream() at MS.Internal.IO.Packaging.SparseMemoryStream.SwitchModeIfNecessary() at MS.Internal.IO.Packaging.CompressEmulationStream.Write(Byte[] buffer, Int32 offset, Int32 count) at MS.Internal.IO.Packaging.CompressStream.Write(Byte[] buffer, Int32 offset, Int32 count) at MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.Write(Byte[] buffer, Int32 offset, Int32 count) at MS.Internal.IO.Zip.ZipIOModeEnforcingStream.Write(Byte[] buffer, Int32 offset, Int32 count) at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder) at System.IO.StreamWriter.Write(String value) at System.Xml.XmlTextEncoder.Write(String text) at System.Xml.XmlTextWriter.WriteString(String text) at System.Xml.XmlAttribute.WriteContentTo(XmlWriter w) at System.Xml.XmlAttribute.WriteTo(XmlWriter w) at System.Xml.XmlElement.WriteTo(XmlWriter w) at System.Xml.XmlElement.WriteContentTo(XmlWriter w) at System.Xml.XmlElement.WriteTo(XmlWriter w) at System.Xml.XmlElement.WriteContentTo(XmlWriter w) at System.Xml.XmlElement.WriteTo(XmlWriter w) at System.Xml.XmlElement.WriteContentTo(XmlWriter w) at System.Xml.XmlElement.WriteTo(XmlWriter w) at System.Xml.XmlDocument.WriteContentTo(XmlWriter xw) at System.Xml.XmlDocument.Save(Stream outStream) at OfficeOpenXml.ExcelWorksheet.Save() at OfficeOpenXml.ExcelWorkbook.Save() at xxx.addDataSetToExcel(DataSet DataSetSource, String fileName, String title, String subject, String category, String comments, String fileType)
Turns out that this is in no way a common problem (google was not my friend on this occasion). After nearly an entire day trying to figure this one out I finally came accross the solution and intend to share it here:
The openXML library uses a MemoryStream object when creating the spreadsheet – When the amount of data your putting into your spreadsheet gets too large to be held in memory, it starts to offload the data to disk. This is where the problem arises.
For whatever reason, ASP.NET decides it’s going to try to put the data into an obscure directory that doesn’t always exist – C:\Documents and Settings\Default User\Local Settings\Application Data\IsolatedStorage. I’m not entirely sure why it comes up with Access is Denied, presumabley because the user in the application pool doesn’t have permission to create a directory?
In any case, if you get this problem, just create the above directory and make sure that the user in your application pool (or Everyone) has read and write access to it.
One response so far
Thanks a lot. This was the most unexpected error and this post helped us to resolve it.