I have the below code in a .Net 4 Winforms app which loads an assembly. All files are on a C:. There are numerous DLL's which work fine but two error with the following:
An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.
This only seems to be a problem on some PCs
Here is the code:
strDLLs = Directory.GetFileSystemEntries(strPath, "*.dll")
For intIndex = 0 To strDLLs.Length - 1
Try
objDLL = [Assembly].LoadFrom(strDLLs(intIndex))
ExamineAssembly(objDLL, strInterface, Plugins)
Catch e As Exception
' MsgBox("Error whilst loading Library: " & strDLLs(intIndex) & ". Reported Error was:" & vbCrLf & e.ToString)
End Try
Next
Well turns out the issue is because the file was possibly downloaded from the internet.
To fix Right Click -> Properties -> Unblock
This is how I managed to get it to work, without resorting to any clicking on client side:
var appDomain = AppDomain.CreateDomain(assemblyName);
var assembly = appDomain.Load(File.ReadAllBytes(assemblyName));
Keep in mind if you CreateDomain with Evidence parameter, you will get the 'This method uses CAS policy, which has been obsoleted by the .NET Framework.' message.
Alternatively, you can set up a proper sandbox:
http://msdn.microsoft.com/en-us/library/bb763046.aspx http://blogs.msdn.com/b/shawnfa/archive/2005/08/08/449050.aspx
Piggybacking on Jon, I had this problem but with lots of assemblies in many different folders. I downloaded Streams from Sysinternals to unblock the files en masse. I found a good discussion on Super User about this topic.
Streams from Sysinternals Super User discussion
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With