How to detect if another assembly is available to my application or not

会有一股神秘感。 提交于 2019-12-11 07:48:42

问题


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

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:

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: 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].

Edit:

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;
   }
}

doesnt writeln either.


回答1:


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




回答2:


Could you not simply look for the file with FileInfo.Exists?



来源:https://stackoverflow.com/questions/924688/how-to-detect-if-another-assembly-is-available-to-my-application-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!