Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load an .exe as a .NET assembly?

Can I just use?:

Assembly.LoadFile

Not sure if this is the way to do this?

But when I try that approach, it throws a Could not load file or assembly "CustomControlLib" or one of its dependencies. The system cannot find the file specified.

Any ideas?

like image 315
Joan Venge Avatar asked Apr 26 '11 22:04

Joan Venge


People also ask

Can EXE be used as DLL?

1. EXE is an extension used for executable files while DLL is the extension for a dynamic link library. 2.An EXE file can be run independently while a DLL is used by other applications. 3.An EXE file defines an entry point while a DLL does not.

Is an EXE an assembly?

An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies take the form of executable (.exe) or dynamic link library (. dll) files, and are the building blocks of . NET applications.

What is EXE in .NET framework?

NET Framework when we compile a Console Application or a Windows Application, it generates EXE, whereas when we compile a Class Library Project or ASP.NET web application, then it generates DLL. In.NET framework, both EXE and DLL are called assemblies.

What is difference between DLL and assembly in net?

An assembly is a collection of one or more files and one of them DLL or EXE. DLL contains library code to be used by any program running on Windows. A DLL may contain either structured or object oriented libraries. A DLL file can have a nearly infinite possible entry points.


2 Answers

You will need to make sure that the dependencies are also loaded into the app domain. If they aren't located automatically, you can subscribe to AppDomain.AssemblyResolve in order to find and load assemblies manually if needs be.

For example:

private Assembly AssemblyEventHandler(object sender, ResolveEventArgs args)
{
    return locatedAssembly;
}

Also, I would consider using Assembly.LoadFrom, particularly after reading this which has a strong assertion by nobugz and links to some good reading (all dated but ought to still be withstanding for the most part.)

like image 160
Grant Thomas Avatar answered Nov 11 '22 18:11

Grant Thomas


If you use Assembly.LoadFrom() dependencies will be loaded from the directory where assembly is located.

Assembly.LoadFile() is ignoring all other DLLs and exe files in the same folder.

like image 22
Sebastian.Belczyk Avatar answered Nov 11 '22 19:11

Sebastian.Belczyk