Inside my Silverlight 4.0 application (with elevated trust level), I’m trying to access the common templates folder, using the code below:
var folder = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
However, such code throwsSystem.Security.SecurityException:
System.Security.SecurityException was unhandled by user code
Message=File operation not permitted. Access to path '' is denied.
StackTrace:
at System.IO.FileSecurityState.EnsureState()
at System.Environment.InternalGetFolderPath(SpecialFolder folder, SpecialFolderOption option, Boolean checkHost)
at System.Environment.GetFolderPath(SpecialFolder folder)
It seems that accessing any folder besides “My Documents” for the current user would throw this kind of exception – Since the SpecialFolder enum has more values, what are they good for? Is there any way to verify which folder this enum/method looks for, or any other way to access it?
In Silverlight 4, the “My Documents” area and isolated storage are the only two places that OOB apps my arbitrarily read/write from/to. This will change with Silverlight 5, where elevated trust apps will have greater access to disk.
As for why it’s there at all, see the remark in the MSDN Documentation:
It’s worth noting that if you are targeting windows OOB, it is possible to read/write files arbitrarily on disk using COM automation and
Scripting.FileSystemObjectin Silverlight 4: