Skip to main content

System.UnauthorizedAccessException - MemoryStream's internal buffer cannot be accessed

 When trying to read a MemoryStream I got the following error message:

Here is the code I was using:

Couldn't be any simpler but unfortunately it didn't work. 

I couldn't figure out what I did wrong...until I noticed the following in the documentation:

This constructor does not expose the underlying stream. GetBuffer throws UnauthorizedAccessException.

Aha, it seems that the OpenXmlPowerToolsDocument is calling GetBuffer behind the scenes. So we need to use a different constructor: