1
votes

I am trying to invoke a method using Reflection within an exe on a network drive, but I get the following exception.

[2013-07-12 11:58:54 AM] The following error occured :Exception has been thrown by the target of an invocation. Inner Exception :System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) at System.Security.CodeAccessPermission.Demand() at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.StreamWriter.CreateFile(String path, Boolean append) at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize) at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding) at System.IO.File.AppendAllText(String path, String contents, Encoding encoding) at System.IO.File.AppendAllText(String path, String contents) at ccc.ControllerBO.ExecuteTest()

I have read up on the Security Settings, but I do not want to install the invoker EXE and cannot find a solution.

What can I do to overcome this?

2

2 Answers

0
votes

You could copy the executable to a place local to your own assembly and invoke it from there.

0
votes

You can create evidence instance and pass it to LoadForm method, like this:

#if NET35
            var evidence = new System.Security.Policy.Evidence();
            evidence.AddHost(new System.Security.Policy.Url(assemblyPath));
            evidence.AddHost(new System.Security.Policy.Zone(System.Security.SecurityZone.MyComputer));
            Assembly assembly = Assembly.LoadFrom(assemblyPath, evidence);                        
#elif NET40
            var evidence = new System.Security.Policy.Evidence();
            evidence.AddHostEvidence(new System.Security.Policy.Url(assemblyPath));
            evidence.AddHostEvidence(new System.Security.Policy.Zone(System.Security.SecurityZone.MyComputer));
            Assembly assembly = Assembly.LoadFrom(assemblyPath);                        
#endif

where assemblyPath - is a full path to file with an assembly (for e.g. on network drive).