Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loadFromRemoteSources error using Assembly.LoadFrom

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
like image 756
Jon Avatar asked Nov 25 '11 11:11

Jon


3 Answers

Well turns out the issue is because the file was possibly downloaded from the internet.

To fix Right Click -> Properties -> Unblock

enter image description here

like image 73
Jon Avatar answered Sep 23 '22 06:09

Jon


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

like image 23
Vedran Avatar answered Sep 20 '22 06:09

Vedran


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

like image 26
Ryan Rodemoyer Avatar answered Sep 23 '22 06:09

Ryan Rodemoyer