loadFromRemoteSources error using Assembly.LoadFrom

15,280

Solution 1

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

Solution 2

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

Solution 3

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

Share:
15,280
Jon
Author by

Jon

Updated on July 28, 2022

Comments

  • Jon
    Jon almost 2 years

    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
    
  • Ryan Rodemoyer
    Ryan Rodemoyer about 12 years
    This solved my problem when I got this error in the designer in Visual Studio.