使用 Assembly.LoadFrom 时发生 loadFromRemoteSources 错误
我在 .Net 4 Winforms 应用程序中有以下代码来加载程序集。所有文件都位于 C: 上。有许多 DLL 可以正常工作,但会出现以下两个错误:
尝试从网络位置加载程序集 会导致程序集在以前的版本中被沙箱化 .NET 框架。本次发布的 .NET Framework 默认情况下不启用 CAS 策略,因此这 负载可能会产生危险。如果此负载不是为了沙盒 程序集,请启用 loadFromRemoteSources 开关。看 http://go.microsoft.com/fwlink/?LinkId=155569了解更多信息信息。
这似乎只是某些 PC 上的问题
以下是代码:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
事实证明,问题是因为该文件可能是从互联网下载的。
修复右键单击->属性->取消阻止
Well turns out the issue is because the file was possibly downloaded from the internet.
To fix Right Click -> Properties -> Unblock
这就是我设法让它工作的方法,无需在客户端进行任何点击:
请记住,如果您使用 Evidence 参数创建域,您将得到“此方法使用 CAS 策略,该策略已被 .NET Framework 废弃” .'信息。
或者,您可以设置适当的沙箱:
http://msdn.microsoft.com /en-us/library/bb763046.aspx
http://blogs.msdn.com/b/shawnfa /archive/2005/08/08/449050.aspx
This is how I managed to get it to work, without resorting to any clicking on client side:
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
依靠乔恩,我遇到了这个问题,但在许多不同的文件夹中有很多程序集。我从 Sysinternals 下载了 Streams 以批量解锁文件。我在超级用户上找到了关于这个主题的很好的讨论。
来自 Sysinternals 的流
超级用户讨论
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
感谢您的精彩解释。
我想添加一些交叉引用信息,这个问题是如何在 VS2015 上出现的,其中没有显示有用的消息。
我使用一个编译的程序集,它本身包含一个程序集。包含的程序集设置了互联网标志。
结果是,应用程序在没有通知的情况下关闭。
使用 VS 2022 时,我收到了这篇初始文章的有用消息。
使用VS 2015时,出现以下错误(翻译自德语):
生成的堆栈跟踪如下:
Thanks for the great explanation.
I want to add some cross-reference information, how this issue arises on VS2015, where the helpful message is not shown.
I use a compiled assembly which itself includs an assembly. The included assembly has the internet flag set.
The result is, that the application closes without notice.
When using VS 2022, I get the helpful message of this initial post.
When using VS 2015, there is the following error (translated from German):
The resulting stack trace is as follows: