There is a solution with two projects.
DLLlibraryWPFapplication usingDLLinReferences
When compiling a WPF project, the DLL is placed in the folder with the EXE . However, for reasons beyond my control, it is necessary that the WPF application look for this DLL in the [Π½ΠΎΠΌΠ΅Ρ Π²Π΅ΡΡΠΈΠΈ] folder in the application folder, ie:
If the application is located in the folder c:\MyProject\project.exe , and the file version 1.0.3.24 means that the DLL will be located in c:\MyProject\1.0.3.24\MyLibrary.dll
Here is my solution:
The Build Action property of App.xaml set to Page , and the code is added to App.xaml.cs
[STAThread] public static void Main() { try { //ΠΠ΅ΡΡΠΌ Assembly ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); //ΠΠ΅ΡΡΠΌ ΠΏΠ°ΠΏΠΊΡ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ string appDirectory = System.IO.Path.GetDirectoryName(assembly.Location); //ΠΠ΅ΡΡΠΌ Π²Π΅ΡΡΠΈΡ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ string appVersion = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion; //ΠΡΡΠΈΡΠ»ΡΠ΅ΠΌ ΠΏΠ°ΠΏΠΊΡ Ρ DLL string dllpatch = System.IO.Path.Combine(appDirectory, appVersion); //ΠΠ΅ΡΠ΅ΠΌ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ ΠΎΠΊΡΡΠΆΠ΅Π½ΠΈΡ PATH string environmentPATH = Environment.GetEnvironmentVariable("PATH"); //Π£ΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅ΠΌ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ ΠΎΠΊΡΡΠΆΠ΅Π½ΠΈΡ PATH Environment.SetEnvironmentVariable("PATH", environmentPATH + ";" + dllpatch, EnvironmentVariableTarget.Process); //ΠΠ°ΠΏΡΡΠΊΠ°Π΅ΠΌ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ var application = new App(); application.InitializeComponent(); application.Run(); } catch (Exception ex) { //ΠΡΠ° ΡΡΡΠΎΠΊΠ° Π½Π΅ ΡΡΠ°Π±Π°ΡΡΠ²Π°Π΅Ρ, Exeption Π½Π΅ ΠΎΡΠ»Π°Π²Π»ΠΈΠ²Π°Π΅ΡΡΡ System.Diagnostics.EventLog.WriteEntry("MyApplication", ex.Message + "\n" + ex.StackTrace, System.Diagnostics.EventLogEntryType.Error); } } However, when you run the project, everything is fine, but as soon as I put the dll in the above folder try catch does not work, the application falls into an exception, and the Windows log is displayed:
Application: project.exe Platform Version: v4.0.30319 Description. The process was terminated due to an unhandled exception. Exception Details: System.IO.FileNotFoundException
in MyNameSpace.App.Main ()
AssemblyResolvetried? - Andrei NOP