关于.NET:使用Assembly.LoadFrom的loadFromRemoteSources错误

loadFromRemoteSources error using Assembly.LoadFrom

我在加载程序集的.Net 4 Winforms应用程序中具有以下代码。 所有文件都在C:上。 有许多DLL都可以正常工作,但是以下两个错误:

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.

这似乎仅在某些PC上是一个问题

这是代码:

1
2
3
4
5
6
7
8
9
10
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


事实证明,原因是该文件可能是从Internet下载的。

要修复右键单击->属性->解除阻止

enter image description here


这是我设法使其起作用的方式,而无需在客户端上进行任何单击:

1
2
var appDomain = AppDomain.CreateDomain(assemblyName);
var assembly = appDomain.Load(File.ReadAllBytes(assemblyName));

请记住,如果使用带有证据的CreateDomain参数,则会得到"此方法使用CAS策略,但.NET Framework已废弃了该方法。" 信息。

另外,您可以设置适当的沙箱:

http://msdn.microsoft.com/zh-CN/library/bb763046.aspx
http://blogs.msdn.com/b/shawnfa/archive/2005/08/08/449050.aspx


在Jon上带,我遇到了这个问题,但是在许多不同的文件夹中都有很多程序集。 我从Sysinternals下载了Streams以取消对文件的整体阻止。 我在超级用户上找到了关于此主题的精彩讨论。

来自Sysinternals的流
超级用户讨论