【问题标题】:Required permissions cannot be acquired error using Assembly.LoadFrom(String) in winforms在 winforms 中使用 Assembly.LoadFrom(String) 无法获取所需权限错误
【发布时间】:2014-12-04 07:04:29
【问题描述】:

我有一个 winforms 应用程序,它在运行时使用 Assembly.LoadFrom(String) 加载一些 dll(我也与应用程序一起编写和安装)。 dll位于exe所在路径的子目录中。

有时,应用程序无法在特定计算机中启动并出现异常“无法加载文件或程序集 'mydll, Version=1.2.4.0, Culture=neutral, PublicKeyToken=null' 或其依赖项之一。失败授予最低权限请求。(来自 HRESULT 的异常:0x80131417)”和“System.Security.SecurityManager.ResolvePolicy(Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, PermissionSet& denied,布尔值 checkExecutionPermission)"。

代码很简单: Assembly^ myAssembly = Assembly::LoadFrom(path);

dll 需要在运行时加载,因为它们是可选的。

我一直在研究这么久,但我发现的所有内容都与 asp.net 有关,这对我没有多大帮助。我没有使用任何 asp.net。 它只发生在我无法访问的非常非常少的机器上

有没有一种方法可以保护应用程序不受此影响并加载 DLL?显然,我可以处理异常,但最终我需要加载 dll。

【问题讨论】:

  • 这是您客户的 IT 人员只能可靠地排除故障的那种问题。他们在部署您的应用程序时做了一些不寻常的事情,在没有完全信任的情况下运行。就像将应用程序复制到不受信任的网络共享(需要 caspol.exe)或通过 Internet 复制文件(在资源管理器中右键单击文件并单击取消阻止)。
  • @VAndrei 这是一个 winforms 应用程序。您的意思是授予对程序文件夹的访问权限?
  • @hansPassant 这也是我的猜测。但希望有办法打败 IT!

标签: winforms c++-cli


【解决方案1】:

你可以试试:

Assembly^ myAssembly = Assembly::LoadFrom(Path::Combine(AppDomain::CurrentDomain->BaseDirectory, path))

【讨论】:

  • 这与 Assembly^ myAssembly = Assembly::LoadFrom(Path) 有何不同,其中 Path 是应用文件夹子目录中 dll 的完整路径?
【解决方案2】:

不能说这个答案是否对你有好处,但我使用了另一种加载机制:

String dllFolder = "folder of DLLs";
DirectoryInfo di = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), dllFolder));
FileInfo[] fia = di.GetFilesByExactExtension("dll", SearchOption.TopDirectoryOnly);
foreach (FileInfo fi in fia)
{
   Byte[] bytes = File.ReadAllBytes(fi.FullName);
   if (bytes.Length > 0)
   {
       Assembly theDLL = Assembly.Load(bytes);
       if (theDLL != null)
       {
           //...optionally do something more with the instance
       }
   }
}

“通过Load() 直接从文件加载”方法的结果好坏参半。

GetFilesByExactExtension() 是一个扩展方法,顺便说一句。

【讨论】:

  • 我认为在这两种情况下,从文件或从流中加载程序集,没有过载,调用者正在将证据传递给程序集。如果我为 CLR 进行文件流传输,您认为是一种不同的内部机制吗?这肯定有助于隔离 IO 错误...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-10
  • 2018-04-26
  • 2020-11-05
  • 2018-09-10
  • 2012-03-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多