Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load file or assembly

This is my Errormessage:

Could not load file or assembly 'file:///C:\Windows\system32\Rule.dll'. The system cannot find the file specified.

The problem is that the same exe works in my development environment but not on productive server. The program is a tool that should run as a Scheduled Task on Windows Server 2008. It consists of an exe, one so called Database.dll and the Rule.dll. The exe should load the Rule.dll dynamically in codebehind but it fails with the above error only when started from Task Scheduler. Why is it looking in System32 folder and not in the Application folder? Is it an UAC-issue?

'Load the rule engine into memory
Dim asmRule As System.Reflection.Assembly = Nothing
Try
  asmRule = System.Reflection.Assembly.LoadFrom("Rule.dll") 'fails on productive system
Catch ex As System.Exception
  HistoryLog.writeLog("SysLog", ex, "Cannot find Rule.dll in Application Folder")
End Try
like image 945
Tim Schmelter Avatar asked Dec 01 '22 05:12

Tim Schmelter


2 Answers

Have you tried:

asmRule = Assembly.LoadFrom(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Rule.dll"));
like image 169
Ta01 Avatar answered Dec 04 '22 23:12

Ta01


The method LoadFrom will use Environment.CurrentDirectory to build the full path to the assembly that will be loaded. The current directory is not the same as the application base path.

You should provide the full path to LoadFrom method, you can build it using AppDomain.CurrentDomain.BaseDirectory if the file is located in the same folder then the executable.

like image 23
João Angelo Avatar answered Dec 05 '22 00:12

João Angelo