如何检测另一个程序集是否可用于我的应用程序 [英] How to detect if another assembly is available to my application or not

查看:190
本文介绍了如何检测另一个程序集是否可用于我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命令行winforms可执行文件,调用Windows winforms应用程序。

I have a command line winforms executable that calls into a Windows winforms application.

部署应用程序时,偶尔会将Windows exe与命令行exe包含在一起,导致错误。

Occassionally Ops forget to include the windows exe with the command line exe when deploying the application, leading to an error.

如何优雅地处理并显示一个很好的错误,而不是:

How do I gracefully handle that and show a nice error instead of:


未处理的异常:
System.IO.FileNotFoundException:可以
无法加载文件或屁股,MyappFoo,
版本= 5.1.4303.0,Culture = neutral,
PublicKeyToken = null'或其
依赖关系之一。系统找不到
指定的文件。文件名:
'MyAppFoo,Version = 5.1.4303.0,
Culture = neutral,PublicKeyToken = null'
在AppFoo.Program.Main(String [] args)

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or ass embly 'MyappFoo, Version=5.1.4303.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. File name: 'MyAppFoo, Version=5.1.4303.0, Culture=neutral, PublicKeyToken=null' at AppFoo.Program.Main(String[] args)

WRN:装配绑定日志记录是
关闭。要启用程序集绑定
失败日志记录,请将注册表
值[HKLM\Software\M
icrosoft\Fusion!EnableLog](DWORD)设置为
1.注意:与组装绑定有关的一些性能损失
失败。要关闭此功能
,请删除注册表值
[HKLM\Software\Microsoft\Fus
ion!EnableLog]。

WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\M icrosoft\Fusion!EnableLog] (DWORD) to 1. Note: There is some performance penalty associated with assembly bind failure lo gging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fus ion!EnableLog].

编辑:

为了澄清一下,如何截取FileNotFoundExcception,当

To clarify a bit, how do I intercept the FileNotFoundExcception, when

static int Main(string[] args)
{
   try
   {
      Console.WriteLine"MyPhooApp Command Line (c) PhooSoft 2008");
   }
   catch (System.IO.FileNotFoundException fe)
   {
      Console.WriteLine("Unable to find foo.exe");
      return -1;
   }
}

也不会写入。

推荐答案

您可以使用.NET 2.0中引入的Reflection-Only加载功能。这样,您可以加载程序集进行检查,而无需将其加载到应用程序域中。

You can use the Reflection-Only loading capability introduced in .NET 2.0. This allows you to load an assembly for inspection without actually loading it into the appdomain.

Assembly.ReflectionOnlyLoad

Assembly.ReflectionOnlyLoadFrom

这篇关于如何检测另一个程序集是否可用于我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆